The XmlDocument class is an in-memory representation of an XML document. It implements the W3C XML Document Object Model (DOM) Level 1 Core and the Core DOM Level 2.

1 Building the document

In some cases, when having a text that corresponds to a XML format, it can be very useful to cover it as a DOM structure to extract node's information, attribute's values, etc.

To do so, the first thing needed is to convert the string into a JSXMLDocument-type using a Document Builder Factory. The mentioned builder can be customized depending on the desired output properties (see further information about XMLDocumentBuilderFactory).

The next example will cover the procedure:

Example

Javascript

Copy
<script>
    const str = `
    <bolsa:inversiones xmlns:bolsa="http://www.bolsa.es" xmlns:geograf="http://www.geograf.com">
        <geograf:país geograf:nombre="España">
            <geograf:capital>Madrid</geograf:capital>
            <bolsa:capital>2000€</bolsa:capital>
        </geograf:país>
    </bolsa:inversiones>`
    
    const dbf = new Ax.xml.DocumentBuilderFactory();
        xml = dbf.parse(str);
    console.log(xml);
</script>

XML

Copy
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bolsa:inversiones xmlns:bolsa="http://www.bolsa.es" xmlns:geograf="http://www.geograf.com">
    <geograf:país geograf:nombre="España">
        <geograf:capital>Madrid</geograf:capital>
        <bolsa:capital>2000€</bolsa:capital>
    </geograf:país>
</bolsa:inversiones>

2 Methods

Objects belonging to JSXMLDocument class have methods that can be useful for XML-type documents.

2.1 DOM Methods

In particular, the following examples are frequently used to navigate through the DOM.

2.1.1 getRootElement

This method returns a JSXMLElement that corresponds to the root-tag of the XML, which allows to traverse the nodes easier moving through child, parents and siblings (see JSXMLDocument documentation).

Following with the beginning XML example:

Example

Javascript

Copy
const str = `
<bolsa:inversiones xmlns:bolsa="http://www.bolsa.es" xmlns:geograf="http://www.geograf.com">
    <geograf:país geograf:nombre="España">
        <geograf:capital>Madrid</geograf:capital>
        <bolsa:capital>2000€</bolsa:capital>
    </geograf:país>
</bolsa:inversiones>`

const dbf = new Ax.xml.DocumentBuilderFactory();
    xml = dbf.parse(str);

console.log(xml.getRootElement());

2.2 Conversion Methods

If needed, a JSXMLDocument can be converted to String, Blob or JSON (among others) using the following methods:

2.2.1 toString

Returns a string representation of the XML document in UTF-8 encoding and including XML declaration.

Example

Javascript

Copy
const str = `
<bolsa:inversiones xmlns:bolsa="http://www.bolsa.es" xmlns:geograf="http://www.geograf.com">
    <geograf:país geograf:nombre="España">
        <geograf:capital>Madrid</geograf:capital>
        <bolsa:capital>2000€</bolsa:capital>
    </geograf:país>
</bolsa:inversiones>`

const dbf = new Ax.xml.DocumentBuilderFactory();
    xml = dbf.parse(str);

console.log(xml.toString());
Copy
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bolsa:inversiones xmlns:bolsa="http://www.bolsa.es" xmlns:geograf="http://www.geograf.com">
    <geograf:país geograf:nombre="España">
        <geograf:capital>Madrid</geograf:capital>
        <bolsa:capital>2000€</bolsa:capital>
    </geograf:país>
</bolsa:inversiones>

A first glance, it could seem the same as the result obtained when converting to JSXMLDocument, but this time the output corresponds to a String, with all the implications it has (different methods, JS primitive type, etc...).

2.2.2 toBlob

A JSXMLDocument can be directly transferred to database as a Blob. This can be particularly useful when the database field-type corresponds to Clob/Blob.

Blob takes the filename according to root element, tag name and extension ".xml". Content type is set as "text/xml".

2.2.3 toJSON

At some point, moving through XML untill finding a concrete node can become a bit tedious, for example, when trying to get the value of a name-known deep node.

In these cases, the JSON notation can be helpful. A JSXMLDocument can be converted to a JSON object easily by calling toJSON().

The returned object is a Java org.json.JSONObject. It can be converted to JavaScript JSON by using toMap() on it.

Using directly this java object and calling get method to search elements, throws an exception if element is not found: json.get("NON-EXISTENT"). You can use json.has("NON-EXISTENT") to verify if element exists before trying to get it.

In the previous example, the capital can be obtained by converting the JSXMLDocument into a JSON JS object and moving through the properties:

Example

Javascript

Copy
const str = `
<bolsa:inversiones xmlns:bolsa="http://www.bolsa.es" xmlns:geograf="http://www.geograf.com">
    <geograf:país geograf:nombre="España">
        <geograf:capital>Madrid</geograf:capital>
        <bolsa:capital>2000€</bolsa:capital>
    </geograf:país>
</bolsa:inversiones>`

const dbf = new Ax.xml.DocumentBuilderFactory();
    xml = dbf.parse(str);

const jsonMapXML = xml.toJSON().toMap();

return jsonMapXML["bolsa:inversiones"]["geograf:país"]["geograf:capital"];
Copy
Madrid
Example

Javascript

Copy
const dbf = new Ax.xml.DocumentBuilderFactory();
str = `<text lang="es" format="html">
        <messages>
            <txt>hello</txt>
            <txt>bye</txt>
        </messages>
      </text>`;

xml  = dbf.parse(str)
json = xml.toJSON();
map  = json.toMap();

if (json.has("text")) {
    console.log(json.get("text").get("messages"));
}
if (map.text) {
    console.log(map.text.messages);
}