3.1.1 • Published 12 months ago

xmlbuilder2 v3.1.1

Weekly downloads
104,347
License
MIT
Repository
github
Last release
12 months ago

xmlbuilder2

An XML builder for node.js.

License NPM Version NPM Downloads jsDelivr

Node.js CI Code Coverage Dev 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"?>
<root att="val">
  <foo>
    <bar>foobar</bar>
  </foo>
  <baz/>
</root>

can be created with the following function chain:

const { create } = require('xmlbuilder2');

const root = 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 string
const xml = 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');

const obj = {
  root: {
    '@att': 'val',
    foo: {
      bar: 'foobar'
    },
    baz: {}
  }
};

const doc = create(obj);
const xml = doc.end({ prettyPrint: true });
console.log(xml);

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

const { create } = require('xmlbuilder2');

const xmlStr = '<root att="val"><foo><bar>foobar</bar></foo></root>';
const doc = create(xmlStr);

// append a 'baz' element to the root node of the document
doc.root().ele('baz');

const xml = 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':

const obj = 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');

const xmlStr = '<root att="val"><foo><bar>foobar</bar></foo></root>';
const obj = convert(xmlStr, { format: "object" });

console.log(obj);
{
  root: {
    '@att': 'val',
    foo: {
      bar: 'foobar'
    }
  }
}

If you need to do some processing:

const { create } = require('xmlbuilder2');

const root = create().ele('squares');
root.com('f(x) = x^2');
for(let i = 1; i <= 5; i++)
{
  const item = root.ele('data');
  item.att('x', i);
  item.att('y', i * i);
}

const xml = root.end({ prettyPrint: true });
console.log(xml);

This will result in:

<?xml version="1.0"?>
<squares>
  <!-- f(x) = x^2 -->
  <data x="1" y="1"/>
  <data x="2" y="4"/>
  <data x="3" y="9"/>
  <data x="4" y="16"/>
  <data x="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 -->
<script src="https://cdn.jsdelivr.net/npm/xmlbuilder2/lib/xmlbuilder2.min.js"></script>
<!-- latest version from unpkg -->
<script src="https://unpkg.com/xmlbuilder2/lib/xmlbuilder2.min.js"></script>

Donations:

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

@monodrom/monodromaggregation-ssml@frondjs/dev-opsplunetsunat-soap-client@dottjt/datareade_data@techstars/contentfulhotelizer_satellitebranch-cli-toolmyfiteco-apimhz-backvideo-templateprojen@everything-registry/sub-chunk-3186@infinitebrahmanuniverse/nolb-xm@haibun/out-xunit@halftome/szamlazz-client@goberman/saml2-js@ivorgri/sveltekit-pluggable-static-adapter-sitemap-plugin@matrix42/cli-bps@matrix42/create-package@lightdark/node-skill-sdk@newfrontdoor/itunes-feed@neurapartner/html-to-docx-typescript@ninanfm/feed@ngageoint/mage.service@la-voz/rss-fbia-with-gtm@la-voz/rss-fbia-with-gtm-feature-block@labelbox/vtk.js@kasual-business/prestashop-api@geolytix/saml2-js@hylimo/diagram-render-svg@hubo99/gdal-asynczoom-products-xmlxml2customjsxomlo-models-prestashopxdvplatform-toolsxdvplatform-walletwsxpwa-frikzwicked-saml2-jswix-msi@haibun/out-review@slick_kilmister/tiny-pdf-server@sonatype/js-sona-types@signed/testcafe-reporter-otr@skiff-org/html-to-docx@skodaflow/sonar-report-merger@tupaca/html-to-docx@open-wa/wa-automate@shoaltogether/saml2-jsn4-simple-build@nqminds/crop-doc-module-m2@olekbruks/sort-resx@nx-boat-tools/dotnet@nudgelabs/html-to-docx-nudgelabs@nxrocks/common-jvm@rdmo-author/xml@recursyve/streamline-icon-set-generator@redso/kiwis@redso/relation@saphrax/cli@saucelabs/cypress-junit-plugin@kitware/vtk.js@klarna/platform-colors@istex/istex-merge@pazy/saml2-js@peertube/saml2-js@pixelplex/sms-service@xomlo/xomlo-models-glami@xomlo/xomlo-models-prestashop@zazuko/trifid-plugin-ckan@elastic.io/saml2-js@yellowtree/webpack-plugin-xlfaditya-app-builder-clinpm-audit-plus-plus@vijhhh2/saml2-js@vijhhh2/saml2jsmxbuildernjs-wa-autoegpx-parser@xmlxyz/rsskit@xcommerceweb/google-merchant-feedehf@ebplants/gis-toolsnode-otris-easnode-opensrsnode-nikerunclub@thoughtindustries/saml2-js@thewtex/vtk.js-esm@timrauhut/feedify@web-atoms/unit-test@empatica/source-map-sbom@empatica/sourcemap-bomfda_submission_packageopayo-reporting-apiopa2junitapex-code-coverage-transformerorcdorg.eclipse.n4js.mangelhaft.reporter.sonar
3.1.1

12 months ago

3.1.0

1 year ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.4.1

3 years ago

2.4.0

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.1.7

4 years ago

2.1.6

4 years ago

2.1.4

4 years ago

2.1.5

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

0.0.13

4 years ago

1.0.0

4 years ago

1.1.0

4 years ago

0.0.14

4 years ago

0.0.12

4 years ago

0.0.11

4 years ago

0.0.10

4 years ago

0.0.9

4 years ago

0.0.7

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago