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
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:
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:
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:
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":
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:
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:
Last updated