OBJECT_TO_XML

The function OBJECT_TO_XML takes an Object and returns a string representing the equivalent XML object.

This function takes a single Object as input. It returns a string which can be parsed as the equivalent object in XML.

This function is also capable of parsing modifiers that define formatting and wrapping elements that have no one-to-one equivalent in Airscript Objects. These modifiers make it possible to use the OBJECT_TO_XML function to create XML objects with roots, namespaces, and more. Modifier details must be specified in a single modifier Object that the OBJECT_TO_XML function accepts as optional input.

Declaration

OBJECT_TO_XML(object, modifier) -> xml

Parameters

object (required, type: Object) Any object.

modifier (optional, type: Object) An object with one or many of the following properties:

  • format (optional, type: boolean, default: FALSE) - Defines whether the returned XML document will be rendered to eliminate whitespace or rendered to be indented and readable. FALSE means that it will be rendered to eliminate whitespace, TRUE means that it won't.

  • root (generally optional but required by namespace, type: string, default: NULL) - Defines the outer element of the entire XML structure.

  • namespace (optional, type: string, default: NULL) - Defines the XML namespace that the XML document will be placed in. This property must be in an Object containing the root property in order to be correctly parsed.

  • attributeNamePrefix (optional, type: string, default: "@") - Defines the string that will distinguish XML elements from XML attributes. Properties of the object prefixed by the attributeNamePrefix will be rendered as XML elements rather than attributes.

Return Values

xml (type: text) The XML value equivalent to the given object.

Examples

When given an Object with only a single key-value pair, the OBJECT_TO_XML function will return a valid XML expression:

OBJECT_TO_XML({ "key": "value" }) -> "<key>value</key>"

However, if the OBJECT_TO_XML function is given an Object with multiple key-value pairs, it will return an invalid XML object without a root:

OBJECT_TO_XML(
  {
    "name": "Joan Smith",
    "title": "CEO",
    "id": "01-0001768"
  }
) -> "<name>Joan Smith</name><title>CEO</title><id>01-0001768</id>"

In order to generate a valid XML object from the Object given above, the root will need to be explicitly specified using a modifier Object with a root property, as follows:

OBJECT_TO_XML(
  {
    "name": "Joan Smith",
    "title": "CEO",
    "id": "01-0001768"
  },
  { "root": "request" }
) -> "<request><name>Joan Smith</name><title>CEO</title><id>01-0001768</id></request>"

The modifier Object can contain multiple properties. For instance, the following example uses:

  • the format property to produced legible, indented XML,

  • the root property to define the outer element of the XML structure as "request", and

  • the namespace property to place the XML document in the namespace "https://tempuri.org":

OBJECT_TO_XML(
  {
    "name": "Joan Smith",
    "title": "CEO",
    "id": "01-0001768"
  },
  {
    "format": TRUE,
    "root": "request",
    "namespace": "https://tempuri.org"
  }
) -> <request xmlns="https://tempuri.org">
  <name>Joan Smith</name>
  <title>CEO</title>
  <id>01-0001768</id>
</request>

The default value of the attributeNamePrefix is "@", which means that properties of the given Object prefixed by "@" will be rendered as XML elements rather than attributes, such as what happens to the id property in the following example:

OBJECT_TO_XML(
  {
    "name": "Joan Smith",
    "title": "CEO",
    "@id": "01-0001768"
  },
  {
    "format": TRUE,
    "root": "contact"
  }
) -> <contact id="01-0001768">
  <name>Joan Smith</name>
  <title>CEO</title>
</contact>

You can also add a attributeNamePrefix property to the modifier Object to define it differently. For instance, the following example defines attributeNamePrefix as "element_". Note how this then results in the character "@" being parsed just like any other character:

OBJECT_TO_XML(
  {
    "name": "Joan Smith",
    "@title": "CEO",
    "element_id": "01-0001768"
  },
  {
    "format": TRUE,
    "root": "contact",
    "attributeNamePrefix": "element_"
  }
) -> <contact id="01-0001768">
  <name>Joan Smith</name>
  <@title>CEO</@title>
</contact>

Last updated