Xml
The Xml module enables parsing information stored in local or remote xml files.
Requires Manatee v2.1 or greater
This version of the Xml module cannot be used with Manatee v2.0 or earlier.
Load xml
Parse the given string as xml and return an XmlDoc object which can be queried or turned into JSON.
Parameters
xmlan xml formatted string to parse
Example
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
urlis a local or remote path to an xml file
Example
// 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 XmlDoc’s json() function.
var json = JSON.stringify({ root: { 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
xpatha well-formed XPath expression
Example
var doc = Xml.load("<hello>world</hello>");
var allHellos = doc.xpath("//hello");If the expression cannot be evaluated against the document, xpath returns null rather than an empty array. Test the result before reading .length off it.
Namespaces
Every namespace declared on the document’s first element is registered for you, under the prefix used in the document. So a document declaring xmlns:nsSBJ="…" can be queried with that prefix directly:
var axes = doc.xpath("//nsSBJ:Axis");A default xmlns cannot be queried without a prefix
To match documents with a namespace declared without a prefix — xmlns="…" — use local name:
var axes = doc.xpath("//*[local-name()='Axis']");Xml to JSON
Returns a JSON/JavaScript version of the document which can then be inspected in the flow.
Example
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.
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 is called for every element in the document, in document order. Text, attribute, comment and CDATA nodes are not visited — if you need the text of an element, read it from the object you are handed.
What you return decides what happens to the node:
- an object or an xml string replaces the node. The replacement is taken as final:
walkdoes not descend into it, so a node you return is never itself visited. falseremoves the node, along with everything inside it.- anything else — including returning nothing — leaves the node alone and
walkcontinues into its children.
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>