XML is the main format of server-side Adobe Campaign JavaScript. You use the XML format every time a 📕 queryDef runs, a SOAP method takes or returns data, a workflow passes a variable, or a web app reads request.data. XML can be read and written to with two classes, part of the DOM API.

DOMDocument and DOMElement

  • DOMDocument is the whole tree — the container you create, parse, or serialize.
  • DOMElement is a single node within it — one tag, with its attributes and children.
  • document.root bridges the two: it is the top DOMElement of a document.
DOMDocument               <- the tree (create / parse / serialize)
└── root : DOMElement     <- the top node
    ├── DOMElement        <- read attributes & children here
    └── DOMElement

DOMDocument and DOMElement extend the abstract class DOMNode.

Where to go

  • 📕 DOMElement to read a node: iterate children, find branches, read attributes.
  • 📕 DOMDocument to create a tree from scratch, from an object, or by parsing XML text.

Use DOM, not E4X

Adobe Campaign’s JavaScript engine historically exposed XML through E4X, which was deprecated in 2012 by Mozilla. Don’t use inline literals (<node/>), for each, and .@attr accessors. See the development best practices.

Migrate from E4X to DOM

To tell whether an XML variable uses E4X, use instanceof XML. If true, it must be migrated to DOM:

// first: check if the XML type is E4X
logInfo(xmlOld instanceof XML);
// then, if true: it uses the deprecated E4X and must be migrated to DOM
var xmlNew;
if (xmlOld instanceof XML) {
  xmlNew = DOMDocument.fromXMLString(xmlOld.toXMLString());
}

E4X is widely used in Adobe Campaign via several features, and each requires a different migration.

  • SOAP custom methods with XML input/output (type)
  • Workflows variables (vars and instance.vars variables)

Unit tests

loadLibrary("/nl/core/shared/nl.js"); // import NL.assert
// Modern dev: XML as DOM
const xmlDom = DOMDocument.fromXMLString("<hello />");
context.e4x.xmlDom.typeof = NL.assert(typeof xmlDom === "object");
context.e4x.xmlDom.instanceof = NL.assert(xmlDom instanceof DOMDocument);
context.e4x.xmlDom.isXML = NL.assert(NL.isXML(xmlDom) === false);
// Deprecated: XML as source
const xmlSrc = <hello />;
context.e4x.xmlSrc.typeof = NL.assert(typeof xmlSrc === "xml");
context.e4x.xmlSrc.instanceof = NL.assert(xmlSrc instanceof XML);
context.e4x.xmlSrc.isXML = NL.assert(NL.isXML(xmlSrc) === true);
// Deprecated: XML as object
const xmlObj = new XML("<hello />");
context.e4x.xmlObj.typeof = NL.assert(typeof xmlObj === "xml");
context.e4x.xmlObj.instanceof = NL.assert(xmlObj instanceof XML);
context.e4x.xmlObj.isXML = NL.assert(NL.isXML(xmlObj) === true);

Deprecated: E4X

The below objects are listed for reference only and should not be used.

isXMLName()

Defined in “ECMA-357 - 13.1.2 - Function Properties of the Global Object”: E4X extends ECMAScript by adding the following function properties to the global object.

logInfo(typeof isXMLName); // function

Namespace

Defined in “ECMA-357 - 13.2 - Namespace Objects”: Namespace objects represent XML namespaces and provide an association between a namespace prefix and a Unique Resource Identifier (URI).

logInfo(typeof Namespace); // function

QName

Defined in “ECMA-357 - 13.3 - QName Objects”: QName objects are used to represent qualified names of XML elements and attributes.

logInfo(typeof QName); // function

XML

Defined in “ECMA-357 - 13.4 - XML Objects”.

The E4X base class to describe an XML node. Now replaced with 📕 DOMElement.

logInfo(typeof XML); // "xml"
var myXml = new XML("<root><child alpha='bravo'>charlie</child></root>");
logInfo(myXml.toXMLString()); // <root><child alpha="bravo">charlie</child></root>
logInfo(myXml.name()); // "root"
logInfo(typeof myXml.child); // "xml"
logInfo(myXml.child.attributes().length()); // 1
logInfo(myXml.child.attribute("alpha").toString()); // "bravo"
logInfo(typeof myXml.child); // "xml"
logInfo(myXml.child.text().toString()); // "charlie"

XMLList

Defined in “ECMA-357 - 13.5 - XMLList Objects”.

The E4X array class to describe a list of XML nodes. Now replaced with 📕 DOMElement.

logInfo(typeof XMLList); // function

typeof

Defined in “11.3.2 The typeof Operator”: E4X reuses the syntax of the ECMAScript’s typeof operator for determining the types of XML and XMLList objects.

JavaScript Type typeof Result
Undefined "undefined"
Null "object"
Boolean "boolean"
Number "number"
String "string"
XML "xml"
XMLList "xml"
Object (native and doesn’t implement [[Call]]) "object"
Object (native and implements [[Call]]) "function"