Parsing XML

For processing incoming XML documents, use xml.parse{} to parse the document into an XML node tree, which you can then manipulate and process as required.

local X = xml.parse{data=SampleXML}

Once the document is parsed, here are some techniques to work with your parsed XML data using the XML API and the XML Library’s extended capabilities:

There are two methods to finding a named element:

  1. Use the XML Library’s node.findElement() to search the full node tree for the specified element name. nil will be returned if not present.

local LabInfo = xml.findElement(X,'lab_info')
  1. Use node.selectElement() to get the element by specifying the parent of the element. Also see how to use the shorthand Colon Operator.

-- Specify the direct parent via the query input 'direct_parent/element_name' X:selectElement('message/lab_info')' -- Or specify the direct parent by calling node.select on X.message X.message:selectElement('lab_info')

Both methods will return the lab_info element. You can use the Annotations to view the results:

Using the XML library’s node.selectElement() you can quickly search through the XML for specific elements using various conditions including a specified:

Repeat:

Attribute Value:

Nested Element Value:

In the Annotations, click through to see the three resulting elements:

Similarly, node.selectElement() can be used to find a specific attribute:

Use node.selectNumber() and node.selectText() to get an element or attributes specified value:

Remember you can also use :nodevalue() for most nodes and :nodetext()to capture text values if you aren’t using the XML library.

 

 

Related pages