xmlbuilder
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
XMLBuilder is a valuable resource for Node.js developers hoping to create XML data. Similar to java-xmlbuilder, this package lets you construct XML structures in a clear and organized manner. The successor xmlbuilder2
is even more powerful with full support for modern DOM specifications, XML namespaces, built-in converters for various formats, and additional features.
To use XMLBuilder, start by installing it in your Node.js project using npm install xmlbuilder
. Once the package is installed, require it at the start of your file with var builder = require('xmlbuilder');
.
Now, you can build XML structures using .create
, .ele
and .end
methods. Here's an example:
var builder = require('xmlbuilder');
var xml = builder.create('root')
.ele('xmlbuilder')
.ele('repo', {'type': 'git'}, 'git://github.com/oozcitak/xmlbuilder-js.git')
.end({ pretty: true});
console.log(xml);
In this example, we are creating a basic XML structure with a root element, a child element xmlbuilder
and a nested child repo
.
You can also convert JavaScript objects into XML structures:
var builder = require('xmlbuilder');
var obj = {
root: {
xmlbuilder: {
repo: {
'@type': 'git',
'#text': 'git://github.com/oozcitak/xmlbuilder-js.git'
}
}
}
};
var xml = builder.create(obj).end({ pretty: true});
console.log(xml);
Here, the structure of the XML is defined by the structure of the JavaScript object.
If you need to do some processing, you can build your XML step by step:
var builder = require('xmlbuilder');
var root = builder.create('squares');
root.com('f(x) = x^2');
for(var i = 1; i <= 5; i++)
{
var item = root.ele('data');
item.att('x', i);
item.att('y', i * i);
}
var xml = root.end({ pretty: true});
console.log(xml);
In this example, we're creating elements in a loop, and setting attributes on them using .att
.
The detailed documentation for XMLBuilder is available on the Github Wiki page. Here you'll find more information on usage, functions and additional examples to help you handle more complex scenarios. Exploring these documents is the best way to become proficient in XMLBuilder.