XML in JDito
This chapter is about working with XML in JDito. XML is often used in APIs or webservices to exchange data in a defined format.
If you have the choice between using XML and JSON, you should always choose JSON as it is native to JavaScript and JDito and it's more lightweight in terms of memory usage. Only use XML if an external API requires it and JSON is not an option.
In JDito you have access to the XML and XMLList module, which can be imported like this:
import { XML } from "@aditosoftware/jdito-types";
This module offers methods for building the XML script in a builder-like way. It can also take a XML script as a string at instantiation to prefill the object based on that. XMLList is used to represent an XML document containing multiple elements and XML is used to represent one element.
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"]);
To access the elements, the typical object notation is used. If you're dealing with simple XML scripts, this is the preferred way. More complex XML scripts may require the usage of the XML object's methods like .child(), .children() or .appendChild() to build or process the XML. As an XML element can contain further elements or even contain several elements having the same name, .child() returns an array of XML objects.
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"] );
}
import { XMLList, XML, logging } from "@aditosoftware/jdito-types";
// Task: A JSON object containing our data needs to be
// "converted" into an XML. (Names are chosen generically.)
var data = {
{
"attribute1":"value1", "attribute2":"value2", "content":"element content"
}
,{
"attribute1":"value1", "attribute2":"value2", "content":"element content"
}
,{
"attribute1":"value1", "attribute2":"value2", "content":"element content"
}
};
// preparing the main element of our XML
var xmlListObj = new XMLList("<data></data>");
// appending children based on the content of your data JSON
for(let obj in data)
{
xmlListObj.appendChild(new XML("<element1 attribute1='" + obj.attribute1 + "' attribute2='" + obj.attribute2 + "'> " + obj.content + "</element1>"));
}
//checking the generated XML in the server log
logging.log(xmlListObj.toXMLString());
Do no longer handle XML using the inline syntax (E4X), example:
// Do not use this!
var myXml = <element1>
<element2>My first text.</element2>
<element3>My second text.</element3>
</element1>;