Skip to main content

XML in JDito

This chapter explains how to work with XML in JDito. XML is still used by some APIs and web services as an exchange format.

note

If you can choose between XML and JSON, prefer JSON. It is native to JavaScript and JDito and usually uses less memory. XML should only be used when an external API requires it and JSON is not available.

JDito provides access to the XML and XMLList types via the following import:

import { XML } from "@aditosoftware/jdito-types";

These types provide builder-like methods for constructing XML. They can also be initialized from an XML string. XMLList represents a document or structure containing multiple elements, while XML represents a single element.

Simple example
import { XML, logging } from "@aditosoftware/jdito-types";

var xmlObject = new XML("<xml> \
<element1 attribute1='value1' attribute2='value2'> \
element1 content \
</element1> \
<xml>");

logging.log("Element1 content: " + xmlObject.element1 + "\nElement1 attribute1: " + xmlObject.element1["attribute1"]);

Elements can be accessed through standard object notation. For simple XML structures, this is usually sufficient. More complex XML documents may require methods such as .child(), .children(), or .appendChild(). Because an XML element can contain nested elements or multiple children with the same name, .child() returns an array of XML objects.

Example of reading an XML containing multiple children of the same name
import { XML, logging } from "@aditosoftware/jdito-types";
//initializing as object from a XML string
var xmlObject = new XML("<xml> \
<element1 attribute1='value1' attribute2='value2'> \
element1 content \
</element1> \
<element1 attribute1='value1' attribute2='value2'> \
element1 content \
</element1> \
<element1 attribute1='value1' attribute2='value2'> \
element1 content \
</element1> \
<xml>");

//iterating over the children of the XML object
for(let child in xmlObject.children())
{
logging.log("Content: " + child.text() + "\nAttribute 1: " + child["attribute1"] + "\nAttribute 2: " + child["attribute2"] );
}
Example of building an XML document
import { XMLList, XML, logging } from "@aditosoftware/jdito-types";

// Task: Convert a JSON-like data structure into XML.

var data = {
{
"attribute1":"value1", "attribute2":"value2", "content":"element content"
}
,{
"attribute1":"value1", "attribute2":"value2", "content":"element content"
}
,{
"attribute1":"value1", "attribute2":"value2", "content":"element content"
}
};

// Prepare the root element.

var xmlListObj = new XMLList("<data></data>");

// Append child elements based on the JSON content.

for(let obj in data)
{
xmlListObj.appendChild(new XML("<element1 attribute1='" + obj.attribute1 + "' attribute2='" + obj.attribute2 + "'> " + obj.content + "</element1>"));
}

// Check the generated XML in the server log.

logging.log(xmlListObj.toXMLString());
warning

Do not use the inline XML syntax (E4X), for example:

// Do not use this!
var myXml = <element1>
<element2>My first text.</element2>
<element3>My second text.</element3>
</element1>;