Skip to content

infinity.xml

Provides XML data parsing and stringifying functionality.

Module: infinity.xml

Functions:

Example:

infinity.loadModule('infinity.xml');

let xmlData = '<note><to>Venus</to><from>Mars</from><heading>Message</heading><body>Line up!</body></note>';

let parsedData = infinity.xml.parse(xmlData);
console.debug(parsedData);
//{"note":{"body":"Line up!","from":"Mars","heading":"Message","to":"Venus"}}

let stringified = infinity.xml.stringify(parsedData);
console.debug(stringified);
//<?xml version="1.0" encoding="UTF-8" standalone="yes"?><note><body>Line up!</body><from>Mars</from><heading>Message</heading><to>Venus</to></note>

Functions

parse()

Attempts to parse the given string according to the XML-format and returns the results as an object.

Every element becomes a property named after the element. Elements that occur more than once on the same level are returned as an array. Attributes are returned as properties prefixed with $ (e.g. id="1" becomes $id), text content of elements that also have attributes or child elements is returned in the property $. Namespace prefixes are kept, the colon is replaced by $ (e.g. soap:Body becomes soap$Body). Text values true, false, null and undefined (case-insensitive) are converted to the corresponding JavaScript values; CDATA sections are always returned as strings. Comments are ignored.

Signature:

parse( value: string, normalize?: boolean, strict?: boolean, convertNumbers?: boolean, stripNameSpaces?: boolean ): any

Parameters:

  • value: string

    The string to parse.

  • normalize: boolean, optional

    Whether to apply normalization: leading and trailing whitespace of text and attribute values is removed and empty text nodes are ignored. Default is false.

  • strict: boolean, optional

    Whether to operate in strict mode. Strict mode true expects correct xml and throws an error on parse errors (e.g. wrong or missing closing tags), while strict mode false can parse html (where tags like or
    don't have a closing tag). Default is false.

  • convertNumbers: boolean, optional

    Whether to convert numerical values within the XML to number types, or otherwise, to treat them as strings. Default is true.

  • stripNameSpaces: boolean, optional

    Whether to remove namespace prefixes from element and attribute names (e.g. soap:Body becomes Body). Default is false.

Return type: any

Example:

infinity.loadModule('infinity.xml');
let parsedData = infinity.xml.parse(xmlData);

stringify()

Serializes the given object according to the XML-format and returns the results. Uses the same conventions as parse(): properties prefixed with $ are written as attributes, the property $ as text content, arrays as repeated elements and $ within names as namespace colon. String values are XML-escaped. Attribute properties of the top-level object (which has no element) and properties starting with # (e.g. #comment) are ignored.

Signature:

stringify( value: object, omitHeader?: boolean ): string

Parameters:

  • value: object

    The object to process. Throws an error if the value is not an object or is an array.

  • omitHeader: boolean, optional

    If set true, then the xml header (<?xml version="1.0" encoding="UTF-8" standalone="yes"?>) is omitted in the returned result. Default is false.

Return type: string

Example:

infinity.loadModule('infinity.xml');
let xmlData = infinity.xml.stringify(dataToStringify);