Following functions exported by xmlbuilder2
are typically imported with:
const { create, fragment, convert, builder } = require('xmlbuilder2');
create
The create
function creates and returns a new XML document. When called without arguments, create
creates an empty XML document (one without any child nodes). create
accepts an optional parameters object for customizing its behavior. These parameters are explained in below. create
also accepts an optional argument defining an XML document to be parsed as detailed in this page.
create(options
: object, contents
: string | object)
options
: object, contents
: string | object)Creates a new XML document by parsing the contents
argument with the given options
and returns the document node.
options
- builder optionscontents
- a string containing an XML document in either XML or JSON format or a JS object representing nodes to insert
const { create } = require('xmlbuilder2');
const doc = create({ encoding: "UTF-8" }, '<root><node/></root>');
console.log(doc.end({ prettyPrint: true }));
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node/>
</root>
create(options
: object)
options
: object)Creates an empty XML document with the given options
and returns the document node.
options
- builder options
const { create } = require('xmlbuilder2');
const doc = create({ encoding: 'UTF-8' });
console.log(doc.end({ prettyPrint: true }));
<?xml version="1.0" encoding="UTF-8"?>
create(contents
: string | object)
contents
: string | object)Creates a new XML document by parsing the contents
argument with the default options and returns the document node.
contents
- a string containing an XML document in either XML or JSON format or a JS object representing nodes to insert
const { create } = require('xmlbuilder2');
const doc = create('<root><foo><bar>foobar</bar></foo></root>');
console.log(doc.end({ prettyPrint: true }));
<?xml version="1.0"?>
<root>
<foo>
<bar>foobar</bar>
</foo>
</root>
create()
Creates an empty XML document with the default options and returns the document node.
const { create } = require('xmlbuilder2');
const doc = create();
console.log(doc.end({ prettyPrint: true }));
<?xml version="1.0"?>
fragment
The fragment
function creates and returns a document fragment node. fragment
accepts the same types of arguments as with the document
function.
fragment(options
: object, contents
: string | object)
options
: object, contents
: string | object)Creates a new document fragment by parsing the contents
argument with the given options
and returns the document fragment node.
options
- builder optionscontents
- a string containing an XML document in either XML or JSON format or a JS object representing nodes to insert
const { fragment } = require('xmlbuilder2');
const frag = fragment({ encoding: 'UTF-8' }, '<node/><node>text</node>');
console.log(frag.toString({ prettyPrint: true }));
<node/>
<node>text</node>
fragment(options
: object)
options
: object)Creates an empty document fragment with the given options
and returns the document fragment node.
options
- builder options
const { fragment } = require('xmlbuilder2');
const frag = fragment({ encoding: 'UTF-8' });
frag.ele('node');
console.log(frag.toString({ prettyPrint: true }));
<node/>
fragment(contents
: string | object)
contents
: string | object)Creates a new document fragment by parsing the contents
argument with the default options and returns the document fragment node.
contents
- a string containing an XML document in either XML or JSON format or a JS object representing nodes to insert
const { fragment } = require('xmlbuilder2');
const frag = fragment('<foo>foo</foo><foo>foobar</foo><bar/>');
console.log(frag.toString({ prettyPrint: true }));
<foo>foo</foo>
<foo>foobar</foo>
<bar/>
fragment()
Creates an empty document fragment with the default options and returns the document fragment node.
const { fragment } = require('xmlbuilder2');
const frag = fragment();
frag.ele('node');
console.log(frag.toString({ prettyPrint: true }));
<node/>
convert
The convert
function converts an XML document into a given format. See
below
and serialization settings
for the options arguments. The input of the convert
function should be a string containing an XML
document in either XML or JSON format or a JS object representing XML nodes.
convert(builderOptions
: object, contents
: string | object, convertOptions
: object)
builderOptions
: object, contents
: string | object, convertOptions
: object)Converts an XML document by parsing the contents
argument with the given
builderOptions
and returns the result formatted with the given convertOptions
.
builderOptions
- builder optionscontents
- a string containing an XML document in either XML or JSON format or a JS object representing nodes to insertconvertOptions
- conversion options
const { convert } = require('xmlbuilder2');
const obj = convert({ encoding: 'UTF-8' }, '<root><node/></root>', { format: 'object' });
console.log(obj);
{ root:
node: { }
}
convert(contents
: string | object, convertOptions
: object)
contents
: string | object, convertOptions
: object)Converts an XML document by parsing the contents
argument with the default
builder options and returns the result formatted with the given convertOptions
.
contents
- a string containing an XML document in either XML or JSON format or a JS object representing nodes to insertconvertOptions
- conversion options
const { convert } = require('xmlbuilder2');
const obj = convert('<root><node/></root>', { format: 'object' });
console.log(obj);
{ root:
node: { }
}
convert(builderOptions
: object, contents
: string | object)
builderOptions
: object, contents
: string | object)Converts an XML document into the default output format by parsing the contents
argument with the given builderOptions
and returns the result.
The default output format is 'xml'
which returns an XML document string.
builderOptions
- builder optionscontents
- a string containing an XML document in either XML or JSON format or a JS object representing nodes to insert
const { convert } = require('xmlbuilder2');
const xml = convert({ encoding: 'UTF-8' }, { root: { node: { } });
console.log(xml);
<?xml version="1.0" encoding="UTF-8"?><root><node/></root>
convert(contents
: string | object)
contents
: string | object)Converts an XML document into the default output format by parsing the contents
argument with the default builder options and returns the result.
The default output format is 'xml'
which returns an XML document string.
contents
- a string containing an XML document in either XML or JSON format or a JS object representing nodes to insert
const { convert } = require('xmlbuilder2');
const xml = convert({ root: { node: { } });
console.log(xml);
<?xml version="1.0"?><root><node/></root>
builder
The builder
function wraps an existing DOM node and returns a builder object.
builder(options
: object, node
: Node)
options
: object, node
: Node)Wraps an existing DOM node with the given options
and returns a builder object.
options
- builder optionsnode
- a DOM node
const { builder } = require('xmlbuilder2');
const node = document.createElement('node');
const xml = builder({ version: '1.0' }, node)
.ele('child')
.end({ prettyPrint: true });
console.log(xml);
<node>
<child/>
</node>
builder(node
: Node)
node
: Node)Wraps an existing DOM node with the default options and returns a builder object.
node
- a DOM node
const { builder } = require('xmlbuilder2');
const node = document.createElement('node');
const xml = builder(node)
.ele('child')
.end({ prettyPrint: true });
console.log(xml);
<node>
<child/>
</node>
Builder options
Settings related to XML declaration
version
- a version number string. Defaults to'1.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.
dec
function.
Settings related to value conversions
keepNullNodes
- whether nodes withnull
andundefined
values will be kept or ignored:true
orfalse
. Defaults tofalse
, which silently ignores nodes withnull
andundefined
values. When set totrue
,null
will be treated as an empty string.keepNullAttributes
- whether attributes withnull
andundefined
values will be kept or ignored:true
orfalse
. Defaults tofalse
, which silently ignores attributes withnull
andundefined
values. When set totrue
,null
will be treated as an empty string.ignoreConverters
- whether converter strings will be ignored when converting JS objects:true
orfalse
. Defaults tofalse
.convert
- an object defining converter strings. Default converter strings are described below.att
- when prepended to a JS object key, converts its key-value pair to an attribute. Defaults to'@'
.ins
- when prepended to a JS object key, converts its value to a processing instruction node. Defaults to'?'
.text
- when prepended to a JS object key, converts its value to a text node. Defaults to'#'
.cdata
- when prepended to a JS object key, converts its value to a CDATA section node. Defaults to'$'
.comment
- when prepended to a JS object key, converts its value to a comment node. Defaults to'!'
.
invalidCharReplacement
- defines a replacement value for invalid characters in input strings. If value is a string, each invalid character in an input string will be replaced with it; otherwise if value is a function it will be passed each invalid character and should return a replacement character. The arguments to the replacement function are:char
- the invalid character to be replacedoffset
- the offset of the invalid characterstr
- the input string
invalidCharReplacement
is limited to the Char
production in the XML spec: #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
.
It doesn’t replace any other character. For example ele('-name')
will still
throw an error, because '-'
is not allowed as a name start character. But the
invalidCharReplacement
option does not replace it for performance reasons.
parser
- defines custom parser functions:parse
- main parser functiondocType
- creates a DocType node from parsed contentcomment
- creates a comment node from parsed contenttext
- creates a text node from parsed contentinstruction
- creates a processing instruction node from parsed contentcdata
- creates a CData node from parsed contentelement
- creates an element node from parsed contentattribute
- creates an attribute or namespace declaration from parsed content
See custom parsers for more information and examples.
Settings related to XML namespaces
defaultNamespace
- contains default namespaces to apply to all elements and attributes (see: example)ele
- default namespace for element nodesatt
- default namespace for attributes
namespaceAlias
- contains namespace aliases where object keys are namespace aliases and object values are namespaces (see: example)