Skip to content

XMLMarshaller

do- edited this page Mar 14, 2022 · 46 revisions

XMLMarshaller is a companion class to XMLSchemata implementing serialization of data objects to XML string according to a given XML schema element (normally, a complex type).

const xs = await XMLSchemata.fromFile ('wsdl_related_stuff.xsd')

const m = xs.createMarshaller (localName, namespaceURI)

const xml = m.stringify (data)

Description

XMLMarshaller can be considered a JSON to XML converter: it does the exactly opposite to XMLNode.toObject, with similar (but inverse) structure transformations.

Additionally, it takes some care about type conversions for scalar values (see the next section).

Type conversions

  • boolean values are always written as false or true; a mapping is predefined for strings '0'/'1', 'false'/'true', 'N'/'Y', other values are subject to standard toBoolean;
  • if a Number or a BigInt is supplied for a date or dateTime field, it's used as an argument for the Date constructor (number of milliseconds since 1970-01-01);
  • for a date field, any string of length 10 is passed through unchecked (it's expected to de in YYYY-MM-DD format, be careful), otherwise the Date object is constructed and (re)serialized;
  • for integer and all its descendants, a BigInt or (for int and shorter types) a Number is created, checked and then serialized;
  • for float and double, a Number is created with parseFloat, then serialized;
  • for decimal too, parseFloat is used, but then, if fractionDigits restriction is set, the string is formed with toFixed;
  • all other values are converted toString, special characters are escaped.

Limitations

  • Probably forever:
    • Virtually no validation of any kind
      • except for edge cases like a true value for a dateTime field: then, XMLMarshaller will throw an error
      • and, maybe, later, for exotic scalar types like gDay)
    • No xsi:type polymorphism
  • Minor performance issues not to be fixed shortly:
    • Namespace prefixes are always prepended, ...FormDefault="unqualified" are ignored.
    • Elements with zero length content have explicit closing tags, self enclosing is not used.
    • " are escaped as " not only in attribute values, but in text nodes too.
  • Pesky things
    • choice is implemented as an alias to sequence: if data are supplied for two or more branches of one choice, the result will be invalid.
    • hexBinary and base64Binary encoding should be supported for Buffer values.

Clone this wiki locally