Generating XML
In general, when building XML messages in the Translator you want to start off with a template XML string. Using an XML template to start building an XML is the easiest and fastest method. It's useful for building complex consistent XML structures as there are often consistent values which can be hardcoded.
-
Parsing an XML Template
Use a simple or complex template depending on the consistency of your XML structure
One or more templates can be used to generate XML messages. It can be a simple template or a more complex template depending on the consistency of your XML structure.
Store a copy of the template XML message locally, either as a string or within a local Lua file.
Let's start with this simple template XML as an example:
local SampleXML = [[
<message>
</message>
]]
The template can then be parsed using xml.parse{} to build a Lua XML node tree and perform your mappings. The template does not need to contain all elements and attributes.
local X = xml.parse{data=SampleXML}
![]()
-
Mapping XML
Functions from the XML API and the XML Library to create elements, attributes, and set values. If you are following along in your Iguana, import the XML Library.
- Add an empty 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. See how Iguana defines the XML Structure and Node Types (e.g.,xml.ELEMENT).
X.message:append(xml.ELEMENT,'observation_info')
-
With the XML Library's
node.addElement(), by providing an element name.
X.message:addElement("extra_info")
If you trace(X), you should see two new elements added to the XML document in the annotations:
![]()
- Add and set an attribute
Use the XML library's node.setAttr() to set an attribute value. If the attribute does not exist, this function will also add the attribute. If an attribute with the same name exists, it will not be added, only the new value will be set.
X.message.extra_info:setAttr("date", os.date('%Y%m%d'))
instead of…
X.message.observation_info:append(xml.ATTRIBUTE,'date') -- add attribute
X.message.observation_info.id:setInner('123') -- set value
![]()
- Add and set text elements and attributes
Use node.setText() to create and set text elements and attributes.
X.message.extra_info:setText("This is some extra text")
![]()
- 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 <>')
![]()
- 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')
![]()
-
Serializing XML
Once the XML message has been generated and mapped, it can be serialized to convert the XML node tree to a Lua string before sending downstream (e.g. to a component queue, in REST or SOAP web request, etc.).
Use tostring() or the node shorthand :S() to serialize XML
local XmlString = tostring(X)
-- or with the XML Library you can use X:S()
![]()
For a more complex example of generating and mapping large XML documents, see the CDA Creator.