ParseXML (parse XML data)
ts
function ParseXML(text: string): object | null;NOTE
This function is available as of Syncplify Server! v7.1.1. If you are running an older version, upgrade to v7.1.1 or later to use it.
Parses an XML string and returns a nested JavaScript object that mirrors the document structure. Element children appear as object properties keyed by element name. When the same element name appears more than once, the value becomes an array of objects. Returns null if the input is not valid XML.
| Parameter | Type | Requirement | Explanation |
|---|---|---|---|
text | string | required | The XML text to parse |
| Return value | Explanation |
|---|---|
| object | Nested JavaScript object representing the XML structure |
null | The input is not valid XML or cannot be parsed |
Conventions used in the returned object
| XML construct | How it appears in the object |
|---|---|
Child element <foo> | Object property foo |
Multiple <foo> siblings | Array at property foo |
Attribute bar="val" on an element | Key _attrs.bar inside that element's object |
| Text content of an element | Key _text inside that element's object |
Example
Given this XML:
xml
<order id="42">
<customer name="Alice" />
<item sku="ABC-1">Widget A</item>
<item sku="ABC-2">Widget B</item>
</order>ParseXML produces an object equivalent to:
json
{
"_attrs": { "id": "42" },
"customer": {
"_attrs": { "name": "Alice" }
},
"item": [
{ "_attrs": { "sku": "ABC-1" }, "_text": "Widget A" },
{ "_attrs": { "sku": "ABC-2" }, "_text": "Widget B" }
]
}Script example
ts
var xml = ReadTextFile('/var/inbox/order.xml');
var doc = ParseXML(xml);
if (doc === null) {
Log('XML parse failed.');
} else {
var orderId = doc['_attrs']['id'];
var items = doc['item']; // array because there are multiple <item> elements
Log('Order ' + orderId + ' has ' + items.length + ' items.');
for (var i = 0; i < items.length; i++) {
Log(' SKU: ' + items[i]['_attrs']['sku'] + ' - ' + items[i]['_text']);
}
}NOTE
ParseXML is designed for straightforward document processing. For very large XML files or streaming use cases, consider using an external tool via RunCapture.
