XML_TO_OBJECT

The function XML_TO_OBJECT takes an XML object in the form of a string and returns the equivalent Object in Airscript.

This function takes a string as input. The given string is expected to be parsable as a valid XML object. It returns the equivalent Object in Airscript.

This function is also capable of parsing modifiers that define formatting and wrapping elements with no one-to-one equivalent in Airscript Objects. These modifiers make it possible to use the XML_TO_OBJECT function to generate Objects that incorporate the information stored in XML elements and attributes such that context is maintained. Modifier details must be specified in a single modifier Object that the XML_TO_OBJECT function accepts as optional input.

Declaration

XML_TO_OBJECT(xml, modifier) -> object

Parameters

xml (required, type: string)
A string that represents a valid object in XML.

modifier (optional, type: Object)

An object with one or many of the following properties:

  • ignoreAttributes (optional, type: boolean, default: TRUE) - Defines whether the resulting will exclude XML attributes when generating the equivalent Object.TRUE means that it will, FALSE means that it won't.
  • attributeNamePrefix (optional, type: string, default: "") - Defines the string that will distinguish XML elements from XML attributes when the equivalent Object is generated. Object properties generated by XML elements will have the attributeNamePrefix prefixing their key, while Object properties generated by XML attributes will not
  • removeNSPrefix (optional, type: boolean, default: TRUE) - Defines whether the prefixes from XML property names will be automatically removed when generating the equivalent Object. TRUE means that they will,FALSE means that they won't.

Return Values

object (type: Object)
The Airscript Object equivalent to the given XML object.

Examples

Assume the following examples has access to the following XML object, which has been pulled in from an external system and converted into a string designated example_xml:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <header>contact</header>
  </soap:Header>
  <soap:Body>
    <contact id="01-0001768">
      <name>Joan Smith</name>
      <title>CEO</title>
    </contact>
  </soap:Body>
</soap:Envelope>

The string example_xml can be given directly as input to the XML_TO_OBJECT function:

XML_TO_OBJECT(example_xml) -> {
  "Envelope": {
    "Header": {
      "header": "contact"
    },
    "Body": {
      "contact": {
        "name": "Joan Smith",
        "title": "CEO"
      }
    }
  }
}

By default, the attributes of the given XML object are ignored when generating the equivalent Object, but this can be changed by using a modifier Object with an ignoreAttributes property, as follows:

XML_TO_OBJECT(
  example_xml,
  { "ignoreAttributes": FALSE }
) -> {
  "Envelope": {
    "Header": {
      "header": "contact"
    },
    "Body": {
      "contact": {
        "name": "Joan Smith",
        "title": "CEO",
        "id": "01-0001768"
      }
    }
  }
}

The Object generated by the above expression does not differentiate which of its properties were generated from XML elements and which of its properties were generated from XML attributes. This can be changed by explicitly defining an attributeNamePrefix property in the modifier Object. For instance, in the following example, Object properties generated from XML elements are prefixed with the string "@", allowing the contextual difference between the XML elements and attributes to be maintained:

XML_TO_OBJECT(
  example_xml,
  {
    "ignoreAttributes": FALSE,
    "attributeNamePrefix": "@"
  }
) -> {
  "Envelope": {
    "Header": {
      "header": "contact"
    },
    "Body": {
      "contact": {
        "name": "Joan Smith",
        "title": "CEO",
        "@id": "01-0001768"
      }
    }
  }
}

The Object generated in the above example does not keep the prefixes from XML property names. To change this, you can use a modifier Object with a removeNSPrefix property, as follows:

XML_TO_OBJECT(
  example_xml,
  {
    "ignoreAttributes": FALSE,
    "attributeNamePrefix": "@",
    "removeNSPrefix": FALSE
  }
) -> {
  "soap:Envelope": {
    "soap:Header": {
      "header": "contact"
    },
    "soap:Body": {
      "contact": {
        "name": "Joan Smith",
        "title": "CEO",
        "@id": "01-0001768"
      }
    },
    "@xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/"
  }
}