You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 4
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:
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!