CollectionsCollections Database

XML Parser

The XML Parser allows to parse and traverse an XML document from Javascript.

You can get the XML document from a request response by calling response.xml(). As an alternative, you can also parse a string with app.xml.parse(string).

response.xml() XML document
Returns an XML document from a request response. It returns undefined if the response can't be parsed.
let request = app.request("https://www.google.com");
let response = request.send();
let xml_document = response.xml();
app.xml.parse(string string) XML document
Creates an XML document from a string. It returns undefined if the string can't be parsed.
let xml_string = '<?xml version="1.0"?><book>Harry Potter</book>';
let xml_document = app.xml.parse(xml_string);

XML Document

xml.document (subclass of xml.node)
This class manages an XML document. It's a subclass of XML Node.
root() XML element
Returns the root element of the document.

XML Node

xml.node
This class manages a generic XML node.
find(expression string) array of nodes
Returns nodes by appling an XPath-like expression to this node. The expression is limited to descendant selectors, other selectors are not currently supported.

Example 1: document.find("/books/book/author");
Find author by following the path starting from the root.

Example 2: book.find("./author");
Find author in the current node.

Example 3: document.find("//author");
Find author anywhere.
type() string
Returns the node type.
Node type: "document", "element", "text", "comment"
string() string
Returns the node content as a string.
child(index number) XML node
Gets the child node at the given index.
childCount() number
Gets the number of children of the node.
prevNode() XML node
Returns the previous node traversing the tree structure.
prevSibling() XML node
Returns the previous sibling (node with the same parent as the current node).
nextNode() XML node
Returns the next node traversing the tree structure.
nextSibling() XML node
Returns the next sibling (node with the same parent as the current node).
parent() XML node
Returns the parent node.

XML Element

xml.element (subclass of xml.node)
This class manages an XML element. It's a subclass of XML Node.
getAttribute(name string) string
Gets an attribute value by its name.

Sample code

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book>
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
    </book>
    <book>
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
    </book>
</bookstore>
let request = app.request("https://www.google.com");
let response = request.send();
if (!response.success) {
    app.fail();
}

let xml_document = response.xml();
let books = xml_document.find("/bookstore/book");

for (let book of books) {
    // Get title node
    let titleNode = book.find("./title")[0];
    // Get title
    let title = titleNode.string();
    // Get language attribute
    let language = titleNode.getAttribute("lang");
    // Get author
    let author = book.find("./author")[0].string();
}