-
Notifications
You must be signed in to change notification settings - Fork 4
XMLPrinter
XMLPrinter is a utility class for generating well formed XML text using a minimalistic low level API.
Depending on the space option, the output can be:
- either onelined (when
spaceis undefined) - or pretty printed, with each nesting level indented with the
spacestring (\tfor instance) orspacenumber of0x20characters.
For pretty printing, os.EOL is used as the default line ending. It's overridable with the eponymous option.
By default, attributes are printed inside the containing opening tags without any breaks, but you can set the additional attrSpace: in this case, each attribute is presented on a new line.
For the sake of readability, all \n and \r characters are replaced with numerical entities for both character nodes and attribute values. Though it's possible to turn off this feature with the encodeLineBreaks option.
XML comments are not supported by the API, neither are DTDs nor PIs (but the XML declaration).
const xp = new XMLPrinter ({
// space: 2,
// attrSpace: 1,
// EOL: '\n',
// level: 0,
// encodeLineBreaks: false,
})| Name | Default | Description |
|---|---|---|
attrSpace |
undefined |
The additional indent for attributes. Similarly to space, can be set as a string or a number. When set, a line is added for each attribute. Otherwise, each opening tag belong to one line |
encodeLineBreaks |
true |
Whether to print line breaking characters as nuberical entities |
space |
undefined |
A literal string or the number of space (0x20) characters to indent opening and closing tags |
level |
0 |
The additional indent level for each string |
EOL |
undefined |
os.EOL by default, this value is used for line breaks |
Hereafter, the methods are listed in typical calling order, not alphabetically.
Prints <?xml version="1.0" ... ?> with encoding and standalone` (when set). Throws an error unless called not first.
Prints <${name} prepended with the necessary whitespace leaving the tag opened for appending attributes.
Prints ${name}="${value}" with the needed spacing and character escaping. Both name, value must be given as strings. Throws an error unless an opening tag is still pending.
For a zero length value, does nothing.
Otherwise, closes the opening tag (if not yet) and prints the escaped value. Throws an error when called before opening of after closing the root element.
Prints the indented </${name}> (with the name taken out from the internal stack) or just />.
Prints out a complete XMLNode, recursively.