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:
- Find a named element
There are two methods to finding a named element:
-
Use the XML Library's
node.findElement()to search the full node tree for the specified element name.nilwill be returned if not present.
local LabInfo = xml.findElement(X,'lab_info')
-
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:
![]()
- Find a specific element using certain conditions
Using the XML library's node.selectElement() you can quickly search through the XML for specific elements using various conditions including a specified:
Repeat:
X.message.patient:selectElement("phone[2]")
Attribute Value:
X.message.patient:selectElement("phone[@type='home']")
Nested Element Value:
X.message.patient:selectElement("address[city='CLEVELAND']")
In the Annotations, click through to see the three resulting elements:
![]()
- Find a specific attribute
Similarly, node.selectElement() can be used to find a specific attribute:
X.message.patient:selectElement("@id")
![]()
- Get an element or attribute's number or text value
Use node.selectNumber() and node.selectText() to get an element or attributes specified value:
X.message.lab_result:selectNumber('weight')
X.message.patient:selectText('@id')
![]()
Remember you can also use :nodevalue() for most nodes and :nodetext() to capture text values if you aren't using the XML library.
X.message.lab_result:nodeValue('weight')
X.message.patient:nodeText('@id')