Ponder2 XML
Ponder2 has an XML managed object which holds XML and can apply XPath queries on it. The XML is a Ponder2 basic managed object and therefore does not need to be loaded. An XML managed object can be created from a String by sending it the "asXML" unary message.
1 xml := "<html><head/><body>Hello, World!</body></html>" asXML.
The following messages are accepted by the XML managed object:
Operation |
Return |
Description |
xpathNodeSet: anXPathExpression |
Array |
Answers an array with XML elements generated by applying anXPathExpression to the receiver |
xpathNode: anXPathExpression |
XML |
Answers an XML element generated by applying anXPathExpression to the receiver |
xpathString: anXPathExpression |
String |
Answers a string generated by applying anXPathExpression to the receiver |
xpathBoolean: anXPathExpression |
boolean |
Answers a boolean generated by applying anXPathExpression to the receiver |
xpathNumber: anXPathExpression |
int |
Answers a number generated by applying anXPathExpression to the receiver |
asString |
String |
|
asXML |
XML |
Returns itself, used for compatibility with other types |
Given the following XML representation of a Ponder2 event, we will proceed to parse it into a hash managed object:
1 <?xml version='1.0' encoding='UTF-8'?>
2 <p2event name='testevent'>
3 <variable type='Hash'>
4 <element name='phone'>
5 <variable type='String'>1234</variable>
6 </element>
7 <element name='address'>
8 <variable type='String'>London</variable>
9 </element>
10 <element name='name'>
11 <variable type='String'>Fred</variable>
12 </element>
13 </variable>
14 </p2event>
First we have to create the hash and convert the string containing the XML into an XML managed object.
We then pick up the p2event element using XPath, this returns another XML managed object with the focus on the p2event element, from now on we can use relative XPath expressions.
1 hash := #() asHash.
2
3 xml := string asXML.
4
5 event := xml xpathNode: "/p2event".
6
7 eventName := event xpathString: "@name".
8 root print: "event name is " + eventName.
9
10 // Get the 'variable' element
11 var := event xpathNode: "variable".
12
13 // Get an array of 'element' nodes
14 elements := var xpathNodeSet: "element".
15 elements do: [ :xml |
16 // Get the name= attribute
17 name := xml xpathString: "@name".
18 // get the element data
19 value := xml xpathString: ".".
20 root print: "Setting " + name + " to " + value.
21 hash at: name put: value.
22 ].
We can now get the name of the event from the attribute followed by the variable structure which contains the names and values of the variables in the hash.
Since we have an array of elements we can cycle through them, taking the name and value from each, and add them to the hash. Note that although the block is a closure and it cannot affect the values of variables outside its context, it can affect the contents of other managed objects. So, in this case it can change the contents of the has because hash is a reference to the actual managed object.
We now have in the hash a proper representation of the XML structure. This structure while being simple is a little more complicated than is required because it fits in with the xmlBlaster example.