-
Notifications
You must be signed in to change notification settings - Fork 4
XMLMarshaller
do- edited this page Dec 3, 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 /*, alternativeLocalName*/)XMLMarshaller can be considered a JSON to XML converter: it does the almost exactly opposite to XMLNode.toObject, with similar (but inverse) structure transformations.
A special object with null key is provided for dealing with any and anyAttribute types used in schemas like SOAP 1.1 that allow XML fragments of totally unknown structure.
xs.stringify ({
Envelope: {
Body: {
null: {
'<someRequest>CODE</someRequest>': // raw XML content to be injected
{Id: 1 /*, ...*/} // attributes key/value pairs
}},
}
}
)yields
<ns0:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/">
<ns0:Body Id="1"><someRequest>CODE</someRequest></ns0:Body>
</ns0:Envelope>Additionally, it takes some care about type conversions for scalar values (see the next section).
-
booleanvalues are always written asfalseortrue; a mapping is predefined for strings'0'/'1','false'/'true','N'/'Y', other values are subject to standardtoBoolean; - if a Number or a BigInt is supplied for a
dateordateTimefield, it's used as an argument for theDateconstructor (number of milliseconds since 1970-01-01); - for a
datefield, any string of length 10 is passed through unchecked (it's expected to de inYYYY-MM-DDformat, be careful), otherwise theDateobject is constructed and (re)serialized; - for
integerand all its descendants, a BigInt or (forintand shorter types) a Number is created, checked and then serialized; - for
floatanddouble, a Number is created withparseFloat, then serialized; - for
decimaltoo,parseFloatis used, but then, iffractionDigitsrestriction is set, the string is formed withtoFixed; - all other values are converted
toString, special characters are escaped.
- Probably forever:
- Virtually no validation of any kind
- except for edge cases like a
truevalue for adateTimefield: then, XMLMarshaller will throw an error - and, maybe, later, for exotic scalar types like gDay)
- except for edge cases like a
- No
xsi:typepolymorphism
- Virtually no validation of any kind
- 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.
- Namespace prefixes are always prepended,
- Pesky things
-
choiceis implemented as an alias tosequence: if data are supplied for two or more branches of onechoice, the result will be invalid. -
hexBinaryandbase64Binaryencoding should be supported forBuffervalues.
-