Skip to content

Repository files navigation

xmlbuilder2

This is NPR's fork of xmlbuilder2, an XML builder for node.js. The project appears to have been abandoned, or at least it enjoys minimal maintenance. The primary impetus for forking is to include our fix for an issue encoding ampersands, though we hope our fix gets incorporated upstream eventually. This library is an important dependency of the Feed Publish Service.

LicenseNPM VersionNPM DownloadsjsDelivr

Node.js CICode CoverageDev Dependency Status

Installation:

npm install xmlbuilder2

Documentation:

See: https://oozcitak.github.io/xmlbuilder2/

Usage:

xmlbuilder2 is a wrapper around DOM nodes which adds chainable functions to make it easier to create and work with XML documents. For example the following XML document:

<?xml version="1.0"?>
<rootatt="val">
<foo>
<bar>foobar</bar>
</foo>
<baz/>
</root>

can be created with the following function chain:

const{ create }=require('xmlbuilder2');constroot=create({version: '1.0'}).ele('root',{att: 'val'}).ele('foo').ele('bar').txt('foobar').up().up().ele('baz').up().up();// convert the XML tree to stringconstxml=root.end({prettyPrint: true});console.log(xml);

The same XML document can be created by converting a JS object into XML nodes:

const{ create }=require('xmlbuilder2');constobj={root: {'@att': 'val',foo: {bar: 'foobar'},baz: {}}};constdoc=create(obj);constxml=doc.end({prettyPrint: true});console.log(xml);

xmlbuilder2 can also parse and serialize XML documents from different formats:

const{ create }=require('xmlbuilder2');constxmlStr='<root att="val"><foo><bar>foobar</bar></foo></root>';constdoc=create(xmlStr);// append a 'baz' element to the root node of the documentdoc.root().ele('baz');constxml=doc.end({prettyPrint: true});console.log(xml);

which would output the same document string at the top of this page.

Or you could return a JS object by changing the format argument to 'object':

constobj=doc.end({format: 'object'});console.log(obj);
{root: {'@att': 'val',foo: {bar: 'foobar'},baz: {}}}

You can convert between formats in one go with the convert function:

const{ convert }=require('xmlbuilder2');constxmlStr='<root att="val"><foo><bar>foobar</bar></foo></root>';constobj=convert(xmlStr,{format: "object"});console.log(obj);
{root: {'@att': 'val',foo: {bar: 'foobar'}}}

If you need to do some processing:

const{ create }=require('xmlbuilder2');constroot=create().ele('squares');root.com('f(x) = x^2');for(leti=1;i<=5;i++){constitem=root.ele('data');item.att('x',i);item.att('y',i*i);}constxml=root.end({prettyPrint: true});console.log(xml);

This will result in:

<?xml version="1.0"?>
<squares>
<!-- f(x) = x^2 -->
<datax="1"y="1"/>
<datax="2"y="4"/>
<datax="3"y="9"/>
<datax="4"y="16"/>
<datax="5"y="25"/>
</squares>

Usage in the browser:

You can build the minified production bundle (lib/xmlbuilder2.min.js) after cloning the repository and issuing npx webpack in your terminal. The bundle is also in the npm package, so you can also use a public npm CDN like jsDelivr or unpkg:

<!-- latest version from jsDelivr --><scriptsrc="https://cdn.jsdelivr.net/npm/xmlbuilder2/lib/xmlbuilder2.min.js"></script><!-- latest version from unpkg --><scriptsrc="https://unpkg.com/xmlbuilder2/lib/xmlbuilder2.min.js"></script>

Donations:

Please consider becoming a backer or sponsor to help support development.

Donate Button

About

An XML builder for node.js

Resources

Code of conduct

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages