Working with XML

The Integration Builder includes the following libraries that allow you to manipulate XML payloads. These are pure-JavaScript libraries sourced from "npm" (the package manager for Node.js), and packaged for use by the Nashorn engine using "browserify".

Library Description

This is an implementation of the W3C DOM parser specification. The source library is XMLDOM (

The DOMParser is a low-level parser used by the JXON and xpath libraries below, but can also be used directly.

XMLUtils.xpath This library allows you to read values from an XML DOM using the xpath syntax. The source library is xpath (

This library can be used to convert an XML document object into a native JavaScript object for further manipulation. The source library is jxon (

The Integration Builder provides a simplifying wrapper object for XMLUtils.JXON that takes an XML-formatted string and returns a JavaScript object using a single method, parse.

The wrapper takes this:

var obj = XMLUtils.DOMParser().parseFromString(xml));

And allows you to do this:

var obj = JXON.parse(xml);


The following examples illustrate how to use the included libraries to manipulate incoming XML payloads.

Incoming XML Manipulation
<incident importance="High">
    <summary>Mail server is down</summary>

Using DOMParser

var xml = request.body;
var doc = new XMLUtils.DOMParser().parseFromString(xml);['Incident ID'] = doc.getElementsByTagName("ticket")[0]; = doc.getElementsByTagName("summary")[0];
var recipients = [];
var recips = doc.getElementsByTagName("username");
for (var i = 0; i < recips.length; i++) {
    recipients.push({"targetName": recips.item(i)});
trigger.recipients = recipients;
trigger.priority = doc.getElementsByTagName("incident")[0].getAttribute("importance");;

Using xpath:

var str = request.body;
// Build the XML document from the request
var doc = new XMLUtils.DOMParser().parseFromString(str, 'text/xml');
// Get the ticket number
var ticket ='//ticket/text()', doc)[0].data;['Incident ID'] = ticket;
console.log("Ticket: " + ticket);
// Get the summary
var summary ='//summary/text()', doc)[0].data; = summary;
console.log("Summary: " + summary);
// Loop over the recipients objects
var recipients ='//recipients/username', doc);
for (var i in recipients) {
    var recipientUsername = recipients[i];
    console.log("Adding recipient " + recipientUsername);
// Get the priority
var importance ='//incident/@importance', doc)[0].value;
console.log("Priority: " + importance);
trigger.priority = importance;
// Post the trigger to the form;

Using JXON:

var xml = request.body;
var obj = JXON.parse(xml);
/* The following data gets logged:
    "incident": {
        "@importance": "High",
        "recipients": {
            "username": [
        "summary": "Mail server is down",
        "ticket": 28088
var data = obj.incident;['Incident ID'] = data.ticket; = data.summary;
var recipients = [];
for (var i in data.recipients.username) {
    recipients.push({"targetName": data.recipients.username[i]});
trigger.recipients = recipients;
trigger.priority = data['@importance'];;
         <sch:clientTimestamp xsi:nil="true"
         <sch:clientIP xsi:nil="true"
         <sch:clientOSUser xsi:nil="true"
               <sch:name>Incident ID</sch:name>
               <sch:value>testing SOAP endpoint</sch:value>

Using xpath with namespace:

var doc = new XMLUtils.DOMParser().parseFromString(request.body);
var select = XMLUtils.xpath.useNamespaces({"sch": ""});
var tokens = select('//sch:eventTokens/sch:eventToken', doc);
// Iterate the event tokens to build the form properties
for (var i in tokens) {
  var nameToken = select('sch:name/text()', tokens[i])[0].data;
  var valueToken = select('sch:value/text()', tokens[i])[0].data;
  console.log("Adding property: " + nameToken + " = " + valueToken);[nameToken] = valueToken;
// Extract the target name from the incoming request and add to the trigger
var targetName = select('//sch:user/text()', doc)[0].data;
console.log("Adding Recipients: " + targetName);
var recipients = [];
    'targetName': targetName
trigger.recipients = recipients;
// Post the form to xMatters;