Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
xmlbuilder2
Advanced tools
The xmlbuilder2 npm package is a powerful tool for building XML documents programmatically. It allows users to create XML documents from JavaScript objects, convert between XML and JS/JSON, and manipulate XML documents with a simple and fluent API.
Creating XML documents
This feature allows users to create XML documents by defining a JavaScript object structure that represents the XML hierarchy. Attributes are prefixed with '@', and text content is represented with '#text'.
{"root":{"@att":"value","child":{"#text":"content"}}}
Converting XML to JS/JSON
This feature enables users to convert an XML string into a JavaScript object or JSON format, making it easier to manipulate or extract data from XML documents.
"<root att='value'><child>content</child></root>"
Manipulating XML documents
Users can manipulate existing XML documents by modifying the JavaScript object representation of the XML. This includes changing attribute values, text content, and adding or removing elements.
{"root":{"child":{"@att":"newvalue","#text":"newcontent"}}}
xml2js is a similar package that provides the ability to parse XML into a JavaScript object and vice versa. It is known for its simplicity and ease of use but may not offer the same level of control over the XML creation process as xmlbuilder2.
fast-xml-parser is another alternative that focuses on speed and performance. It offers parsing and validation of XML data and can handle large XML files efficiently. However, it might not have as fluent an API for building XML documents as xmlbuilder2.
xml-js provides conversion between XML and JSON formats. It allows users to convert XML documents into a JSON representation and back, similar to xmlbuilder2, but it may not have as many features for manipulating or creating XML documents.
An XML builder for node.js.
npm install xmlbuilder2
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 { document } = require('xmlbuilder2');
const root = document({ 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 { document } = require('xmlbuilder2');
const obj = {
root: {
'@att': 'val',
foo: {
bar: 'foobar'
},
baz: {}
}
};
const doc = document(obj);
const xml = doc.end({ prettyPrint: true });
console.log(xml);
xmlbuilder2
can also parse and serialize XML documents from different formats:
const { document } = require('xmlbuilder2');
const xmlStr = '<root att="val"><foo><bar>foobar</bar></foo></root>';
const doc = document(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: {}
}
}
If you need to do some processing:
const { document } = require('xmlbuilder2');
const root = document().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>
See the wiki for details.
FAQs
An XML builder for node.js
We found that xmlbuilder2 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.