You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 3
Next »
The XML Techniques component can be used to demonstrate strategies for parsing and generating XML documents in IguanaX using the XML Library. It is highly recommended to import the XML library to extend and ease working with XML documents. Let’s break down these strategies one by one:
Parsing XML:
For processing incoming XML documents, use xml.parse to parse the document into an XML node tree.
local X = xml.parse{data=SampleXML}
Once to document is parsed, here are some techniques to work with your parsed XML data:
1) 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. nil
will 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.
-- 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:
2) 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:
3) Find a specific attribute
Similarly, node.selectElement()
can be used to find a specific attribute:
X.message.patient:selectElement("@id")
4) 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')
Generating XML:
To build an XML document, use a hardcoded template you can parse and build on. For example:
local SampleXML = [[
<message>
</message>
]]
1) Add an element
There are two methods to adding elements to an existing XML document.
With node.append()
by providing what you want to create and a name.
X.message:append(xml.ELEMENT,'observation_info')
With the XML Library’s node.addElement()
, by providing an element name.
X.message:addElement("extra_info")
You should see two new elements added to the XML document in the annotations.
2) Add and set an attribute
When adding and setting an attribute, you can use the XML library’s node.setAttr()
to create and set an attribute in one line:
X.message.extra_info:setAttr("message_id", iguana.messageId())
instead of…
X.message.observation_info:append(xml.ATTRIBUTE,'id') -- add attribute
X.message.observation_info.id:setInner('123') -- set value
3) Add and set text elements
Use node.setText()
to create and set text elements.
X.message.extra_info:setText("This is some extra text")
4) Add and set CDATA elements
To add and set CDATA you must use node.append()
to specify you are creating a CDATA element and the content to set.
X.message.observation_info:append(xml.CDATA,'Because CDATA is not parsed it allows & and <>')
5) Add an element in a specific location in the xml
You can also add an element in s specific location in the XML document using node.insert()
, specifying the location, an element node type and the content to set.
X.message:insert(1, xml.ELEMENT, 'first')
6) Serialize the xml node tree once ready to push downstream
Finally, once your XML document is ready, in order to queue it for a downstream component, write it to a file or send it in an HTTP request, it must be serialized as a string!