• Stars
    star
    365
  • Rank 116,851 (Top 3 %)
  • Language
    TypeScript
  • Created almost 6 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An XML builder for node.js

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.

Donate Button

More Repositories

1

xmlbuilder-js

An XML builder for node.js
CoffeeScript
916
star
2

imagelistview

A .NET listview control for image files with asynchronously loaded thumbnails.
C#
163
star
3

exiflibrary

A .Net Standard library for editing Exif metadata
C#
115
star
4

SimpleCad

A basic CAD-like control surface for winforms.
C#
98
star
5

akismet-js

Akismet API client for node.js
TypeScript
31
star
6

TabControl

A winforms control with multiple tabs.
C#
27
star
7

WizardControl

Wizard control for winforms
C#
13
star
8

PagedControl

A winforms control with multiple pages.
C#
13
star
9

dom

A modern DOM implementation
TypeScript
11
star
10

eve-corp-portal

A custom CMS for corporations in the game Eve Online
PHP
8
star
11

TriMesh

A delaunay mesh generator.
C#
6
star
12

FileSystemControls

Various shell controls for winforms.
C#
5
star
13

canphotoblog

A dead easy photo blogging application powered by node.js
CoffeeScript
5
star
14

meshgenerator

Automatically exported from code.google.com/p/meshgenerator
C#
4
star
15

uts46

A Javascript implementation of Unicode IDNA Compatibility Processing (UTS 46)
TypeScript
4
star
16

SapCleaner

Batch deletes Sap2000 analysis files
C#
3
star
17

url

An implementation of the URL Living Standard
TypeScript
2
star
18

glcanvas

OpenGL Canvas Control for .Net
C++
2
star
19

infra

An implementation of the Infra Living Standard
TypeScript
2
star
20

util

Utility functions
TypeScript
2
star
21

xmlbuilder-typing-tests

Repository for testing TypeScript API of xmlbuilder.
TypeScript
1
star
22

oozcitak

1
star
23

perf

Performance testing for node.js
JavaScript
1
star
24

dotfiles

The contents of my dotfiles
Vim Script
1
star