Continuing on from Part 1 of this tutorial, let's play a little with XML data using the HEntry techniques discussed.
Here is some example XML that we'll play with:
<member position="bat boy">Lancel</member>
To handle the XML in TDI you could read in a file. A handier approach for play/test is to set up an AL with the FormEntry Connector and paste the encoded string into the Raw Data Text parameter:
FormEntry works like a File Connector, but instead of reading from a file, it uses the data entered into the Raw Data Text parameter.
Add the wildcard map (* map all attributes) to the Input Map and then choose the XML Parser, clearing out the Entry Tag parameter. This tells the parser to return a single Hentry.
From the topmost screenshot you can see that I also dropped an Empty Script in the Data Flow section of my AL. Right click on this empty script and select Run and break here. This launches the AL in the Debugger and runs to this point.
The Debugger first displays the snippet that was executed, followed by >> and then the result. The output above shows this is a TDI Attribute. Attributes display themselves first with their name in quotes followed by a colon (:) and then the value. In the above screenshot the square brackets shown for the value tells us this is a hierarchical Attribute. To get at the children we can use the getChildNodes() method:
children = work.teams.getChildNodes()
A NodeList has only two methods: getLength() and item(). The first one tells you how many nodes are in the list, and the second one is for accessing a node. For example:
for (i = 0; i < children.getLength(); i++) task.logmsg(children.item(i))
This time the output is too long for the log view. So you can press the Page button to view the entire log output in a separate editor.
Note also that instead of using the .item() method, you can also just use square brackets:
And furthermore, you can use the for-in construct as well to enumerate child nodes:
for (child in children) task.logmsg(child.toString());
Post a Comment