SlightlyLoony
Tera Contributor

find_real_file.pngWhen you're working with JavaScript on the client side, it's pretty easy to work with XML — the DOM is built right in. But what if you want to work with XML in a server-side JavaScript? There's no DOM on the server-side!

There are several possibilities, actually. There is quite a bit of support in Java that is also available in JavaScript (through the Packages.xxx), but it is a bit awkward to use, and requires that you (or the developer) know the Java library support as well. Our developer at right thinks that working with XML in this fashion is as terrifying as leaping off a 300 foot high bridge with only a bungee cord to save her.

Wouldn't it be nice to have a way to work with XML on the server side in pure JavaScript?

You should know what's coming next: starting with the upcoming October release (and in the next preview, and in demo.service-now.com right now), you can!

The ServiceNow platform comes with a script include named XMLHelper that greatly simplifies reading, analyzing, and producing XML documents or strings. It takes a different approach than the DOM on client-side scripting — instead of directly navigating through the DOM, XMLHelper converts XML to and from JavaScript objects. Your scripts can then directly read or modify the JavaScript objects with very simple code.

This is one of those things that's best explained by example:


var xml = '<result>';
xml += ' <people>';
xml += ' <person>';
xml += ' <country>';
xml += ' United States';
xml += ' <former>Germany</former>';
xml += ' </country>';
xml += ' <occupation>electrician</occupation>';
xml += ' </person>';
xml += ' <person>';
xml += ' <country>Elbonia</country>';
xml += ' <occupation>cartoonist</occupation>';
xml += ' </person>';
xml += ' </people>';
xml += '</result>';

var xh = new XMLHelper();
var obj = xh.toObject(xml);
JSUtil.logObject(obj, 'Our resulting JavaScript object!');


The first part of this test code is simply constructing a little XML string for us to play with. The last three lines are the interesting part, wherein we construct an instance of XMLHelper, then call its .toObject() method with our XML test string as the argument. The result goes into the obj variable, and then we use JSUtil to log the object. When we run this code, we get this result in the log (or on the screen in Scripts - Background):

*** Script: Log Object: Our resulting JavaScript object!
Object
people: Object
person: Array of 2 elements
[0]: Object
@name: string = Werner Schultz
country: Object
#text: string = United States
former: string = Germany
occupation: null = null
[1]: Object
@name: string = Scott Adams
country: string = Elbonia
occupation: string = cartoonist

Here's a little snippet of code that logs every person and their occupation by reading that object:

for (var i = 0; i < obj.people.person.length; i++) {
var person = obj.people.person;
gs.log(person['@name']);
gs.log(' ' + person.occupation);
}

That was easy! You can also convert a JavaScript object (in this same format) back to XML, like this:

var str = xh.toXMLStr(obj);
gs.log(str);

That means you can create a JavaScript object in your code, following the format shown above, and then make one simple call to XMLHelper to get a nice XML document!

Here are a few thing worth noting about the JavaScript object format:

  • Tags in the XML turn into corresponding JavaScript properties. The value of this property will be one of three things: a string (if the tag had no attributes and contained no other tags), an object (if the tag had attributes or contained other tags), or an array (if there are more than one of the same tag enclosed by a parent tag).
  • Attributes turn into properties whose name is "@" plus the original XML attribute name. These properties always contain a string with the value of the attribute.
  • Tags that enclose both text and other tags (or have an attribute) will turn into properties named "#text". These properties always contain a string with the value of the text.
  • There's no particular limit to the size or depth of the XML document; it is only constrained by available memory (so be careful with really gigantic documents!).
  • XMLHelper can also convert Java Document instances to JavaScript, and vice versa. See the XMLHelper script include if you're interested in this.

4 Comments