Workflow javascript to read XML

I must be missing something here. I know the Workflow script plug-in “enhanced Jscript” isn’t a full javascript interpreter, but I’m having quite a time trying to read xml node and attribute values in javascript. I can craft working samples in jsfiddle or codepen, but when translating that into a Workflow script enough must be different that it isn’t useable.

Here’s a sample script if someone can correct where I’m going wrong. The code below will produce “null” “not an object” or “object required” errors depending on options I try. I’ve tried various options for creating the xml object, but can’t seem to get it right and then read data from nodes and attributes.

var text = "<bookstore><book type=\"Paperback\">" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";

/* tried this as well but didn't work
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
*/
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load(text);

var xTitle = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
Watch.Log(xTitle, 2);
//expected title node result is Everyday Italian
//also tried Watch.Log(xTitle.text, 2);

var xType = xmlDoc.getElementsByTagName("book")[0].getAttribute('type');
Watch.Log(xType, 2);
//expected type attribute result is Paperback

Appreciate the help!

You were close:

var text = "<bookstore><book type=\"Paperback\">" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(text);

var xTitle = xmlDoc.getElementsByTagName("title")[0].text;
Watch.Log(xTitle, 2);
//expected title node result is Everyday Italian
//also tried Watch.Log(xTitle.text, 2);

var xType = xmlDoc.getElementsByTagName("book")[0].getAttribute('type');
Watch.Log(xType, 2);
//expected type attribute result is Paperback

A more elegant alternative to getElementsByTagName() is the selectSingleNode() and selectNodes() methods, which take in a XPATH parameter:

var xTitle = xmlDoc.selectSingleNode("/bookstore/book/title");
Watch.log(xTitle.text,2);

var xType = xmlDoc.selectSingleNode("//book").getAttribute('type');
Watch.Log(xType, 2);

Note that the XMLDOM object is not part of the JavaScript spec. It’s an external object provided in Windows that can be accessed through just about any programming language.

Thanks @Phil!

I still must be missing something. When I update the script to use the selectSingleNode function it still errors out.

var text = "<bookstore><book type=\"Paperback\">" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
//var xmlDoc = new ActiveXObject("msxml2.DomDocument");
xmlDoc.async="false";
xmlDoc.load(text);

var xTitle = xmlDoc.selectSingleNode("/bookstore/book/title");
Watch.log(xTitle.text,2);
//expected title node result is Everyday Italian

var xType = xmlDoc.selectSingleNode("//book").getAttribute('type');
Watch.Log(xType, 2);
//expected type attribute result is Paperback

After creating the xTitle variable (xmlDoc.selectSingleNode(“/bookstore/book/title”)) the Watch.Log statement errors out with:
[0004] W3602 : Error 0 on line 14, column 1: Microsoft JScript runtime error: Object required

Then bypassing that first statement when I try just the attribute (xmlDoc.selectSingleNode(“//book”).getAttribute(‘type’)) the Watch.Log statement errors out with:
[0004] W3602 : Error 0 on line 17, column 1: Microsoft JScript runtime error: ‘xmlDoc.selectSingleNode(…)’ is null or not an object

I’ve tried using different options as well:
Watch.log(xTitle,2); >>> runtime error: Type mismatch
Watch.log(xTitle.text,2); >>> runtime error: Object required
Watch.log(xTitle.textContent,2); >>> runtime error: Object required

I did just run across a different forum post that created the XML object a bit differently.
var xmlDoc = new ActiveXObject("msxml2.DomDocument");
But that still produced the same errors.

You have to use the xmlDoc.loadXML() method to load content from a variable, the xmlDoc.load() method is for xml files. See the code in my original post.

Indeed I did miss that! It’s now working. Appreciate the help!