-
-
Notifications
You must be signed in to change notification settings - Fork 110
Builder Options
Ozgur Ozcitak edited this page Mar 26, 2020
·
8 revisions
This page documents the various options that can be used to customize the behavior of the XML builder.
-
version
A version number string:1.0
or1.1
. This also changes character validation. Defaults to1.0
if omitted. -
encoding
Encoding declaration, e.g.UTF-8
. No encoding declaration will be produced if omitted. -
standalone
standalone document declaration:true
orfalse
. No standalone document declaration will be produced if omitted. -
headless
whether XML declaration and doctype will be included:true
orfalse
. Defaults tofalse
.
Note: XML declaration can be specified later with the dec
function. Also see this wiki page.
builder.create('root')
.dec('1.0', 'UTF-8', true);
-
pubID
public identifier of the external subset. No default. -
sysID
system identifier of the external subset. No default.
Note: If neither pubID
nor sysID
is given, an external document type definition will not be produced.
Note: A DTD can also be created at a later time by calling doctype
from anywhere in the document (can also be abbreviated to dtd
). Also see this wiki page.
var dtd = root.dtd('pubID', 'sysID');
-
keepNullNodes
whether nodes withnull
values will be kept or ignored:true
orfalse
. Defaults tofalse
, which silently ignores nodes withnull
values. When set totrue
,null
will be treated as an empty string. -
keepNullAttributes
whether attributes with null values will be kept or ignored:true
orfalse
. Defaults tofalse
, which silently ignores attributes withnull
values. When set totrue
,null
will be treated as an empty string. -
ignoreDecorators
whether decorator strings will be ignored when converting JS objects:true
orfalse
. Defaults tofalse
. See this page for a list of decorator strings. -
separateArrayItems
whether array items are created as separate nodes when passed as an object value:true
orfalse
. Defaults tofalse
. See this page for an example. -
noDoubleEncoding
whether existing html entities are encoded:true
orfalse
. Defaults tofalse
. For example, when converting the following JS object:
const root = {
'@att': 'attribute value with # and #'
'#text': 'HTML entities for umlaut are ü and ü.'
}
// with noDoubleEncoding: false (default)
const xmlStr = builder.create(obj).end({ pretty: true });
// <?xml version="1.0"?>
// <root att="attribute value with &num; and &#35;">
// HTML entities for umlaut are &uuml; and &#252;.'
// </root>
// with noDoubleEncoding: true
const xmlStr = builder.create(obj, { noDoubleEncoding: true }).end({ pretty: true });
// <?xml version="1.0"?>
// <root att="attribute value with # and #">
// HTML entities for umlaut are ü and ü.'
// </root>
-
noValidation
whether XML character validation will be disabled:true
orfalse
. Defaults tofalse
. -
invalidCharReplacement
sets a character to replace invalid characters in input strings. Defaults toundefined
. Note that setting this option also disables character validation. See XML 1.0, §2.2 Characters and XML 1.1, §2.2 Characters for valid and invalid character definitions.
const obj = {
'node\x00': 'text\x08content'
}
const xmlStr = builder.create(obj, { invalidCharReplacement: '' }).end({ pretty: true });
// <?xml version="1.0"?>
// <node>textcontent</node>
or using a replacement function:
const obj = {
'node\x00': 'text\x08content'
}
const options = {
invalidCharReplacement: (c) => c === '\x00' ? '' : '_'
};
const xmlStr = builder.create(obj, options).end({ pretty: true });
// <?xml version="1.0"?>
// <node>text_content</node>
-
stringify
a set of functions to use for converting values to strings. See this page for value conversion details and decorator strings. -
writer
the default XML writer to use for converting nodes to string. If the default writer is not set, the built-inXMLStringWriter
will be used instead. See this page for information on writers.