Using XML

Many JavaScript APIs take XML type objects as parameters or return them. Previous versions of Adobe Campaign used the JavaScript E4X extension for this which would allow XML elements to be directly manipulated in the language. However, this technology is slowly being abondoned.

To replace this technology, new objects have been added to manipulate XML documents. They resume a subset of the Document Object Model (DOM) interface with some extensions. A notation object is also available to easily create XML documents, it is a simplified version of the JXON BadgetFish convention.

DOM Interface

A DOMDocument object is an XML document. It can be made from a string of characters, a JXON object or directly from a file. Here is an example of creating a DOM document:
var doc  = new DOMDocument("recipient")
var root = doc.root
root.setAttribute("id", 12345)
var firstName = doc.createElement("firstName")
firstName.textContent = "John"
root.appendChild(firstName)

logInfo(doc.toXMLString(true))
This interface gace a part of the W3C specification with various notable differences:
  • The functions getElements* as well as the childNodes and attributes properties return a JavaScript table rather than a NodeList object, this allows use of the tables' own functions: forEach, map, ...
  • Certain properties return an empty string rather than a null when the value has not been specified.
Furthermore, certain utility functuons and some non standard extensions have been added. For example to read or modify an attribute, it is only necessary to write:
aRecipient.$firstName
aRecipient.$firstName = "John"
instead of:
aRecipient.getAttribute("firstName")
aRecipient.setAttribute("firstName", "John")
Assigning an attribute with the $name property can take a string, a boolean, a date or a number, the value will be automatically converted into a string of characters:
aRecipient.$firstName = "John"
aRecipient.$lastModified = new Date()
aRecipient.$age = 35
aRecipient.$blocklist = false

JXON

JXON is a notation convention for JavaScript objects to easily build XML documents. This notation does not allow you to create all XML documents possible. For example, it does not allow you to specify an element with the same name as an attribute and the namespaces are not managed. It is aapted for Adobe Campaign documents. For more complex needs, you can always use traditional DOM functions (parsing or creating element by element). The XML object is represented by a JavaScript object whose properties match the elements and attributes. The value type determines whether it is to do with an element or an attribute.

  • String An attribute whose value is the character string
  • Boolean An attribute whose value is either 'true' or 'false'
  • Number An attribute whose value is the decimal number representation
  • Date An attribute whose valye is the ISO-8601 representation of the date and time
  • Object An element
  • Array A list of elements

A special property under the name of $ is used for an element's text content Examples:

JXON XML
{ alice: { $: "bob" } }
<alice>bob</alice>
{ alice: { 
  bob: { $: "charlie" }, 
  david: { $: "edgar" }
}}
<alice>
  <bob>charlie</bob>
  <david>edgar</david>
</alice>
{ alice: { 
  bob: [
    { $: "charlie" }, 
    { $: "david" } ]
}}
<alice>
  <bob>charlie</bob>
  <bob>david</bob>
</alice>
{ alice: { 
  charlie: "david",
  $: "bob"
}}

              <alice charlie="david">bob</alice>
{ recipient: {
  firstName: "John",
  id: 1234,
  "creation-date": getCurrentDate(),
  blocklist: false
}}
<recipient
  firstName="John"
  id="1234"
  creation-date="2013-04-19 10:58:22Z"
  blocklist="false"/>

When properties contain special characters that are invalid JavaScript identifiers, these must be put in quotation marks.

{ recipient: { "recipient-id" : 1234 }}
      

JXON objects can be used in arguments anywhere where a function anticipates a document or XML element. For example:

var doc = DOMDocument.fromJXON({queryDef: 
    {schema: "xtk:workflow", operation: "select",
      select: { node: {expr: "@internalName"} } 
    }
  })
var query = NLWS.xtkQueryDef.create(doc)

is equivalent to:

var query = NLWS.xtkQueryDef.create({queryDef: 
    {schema: "xtk:workflow", operation: "select",
      select: { node: {expr: "@internalName"} } 
    }
  })

It is important to note that manipulating XML documents is done by DOM objects, the JXON objets are utilities used only for initialization. It is more useful to create an XML document from a literal JavaScript structure, particularly when the document contains variable parts.