Skip to content

Xml

The Xml module enables parsing information stored in local or remote xml files.

Load xml

Parse the given string as xml and return an XmlDoc object which can be queried or turned into JSON.

Parameters

  • xml an xml formatted string to parse

Example

javascript
var d = Xml.load("<hello>world</hello>");

Load XML from url

Fetch a local or a remote file and parse as xml. Returns an XmlDoc object.

Parameters

  • url is a local or remote path to an xml file

Example

javascript
// A remote file
var remote = Xml.loadFrom("http://somewhere/over/the/rainbow.xml");
// A local file
var local = Xml.loadFrom("c:\\somewhere\over\the\rainbow.xml");

Load XML from JSON string

You can use the loadJson function to parse a JSON string into an XmlDoc object. It should have the same structure as the output of the Xml.toJson function.

javascript
var json = JSON.stringify({ a: { b: 2 }, c: 3 }); 
var doc = Xml.loadJson(json);

XmlDoc

An XmlDoc is an object that wraps an xml document and which has a few functions for querying the underlying document.

XPath

Execute an XPath query and return the results. The result is a list of objects, each object represents the matching xml node.

Parameters

  • xpath a well-formed XPath expression

Example

javascript
var doc = Xml.load("<hello>world</hello>");
var allHellos = doc.xpath("//hello");

Xml to JSON

Returns a JSON/JavaScript version of the document which can then be inspected in the flow.

Example

javascript
var doc = Xml.load("<hello>world</hello>");
var docObject = doc.json();

XmlDoc to XML string

Use the toString function to return the xml document as a string.

javascript
var doc = Xml.load("<hello>world</hello>");
var xmlString = doc.toString(); // "<hello>world</hello>"

Modify an XML document

The walk function implements a visitor-pattern for the underlying xml document. It can be used to modify the document. You supply it with a function that may either return a new value as an object or xml string or a boolean value where false indicates that the node should be removed. The function will get called for each node in the document.

javascript
var doc = Xml.load("<root><hello>world</hello></root>");
doc.walk(function(node) {
    if (node.hello) {
        return "<hello>universe</hello>";
    }
});
var greeting = doc.toString(); // <root><hello>universe</hello></root>