Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork37
oozcitak/xmlbuilder2
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
An XML builder fornode.js.
npm install xmlbuilder2
See:https://oozcitak.github.io/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"?><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 theformat 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 theconvert 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>
You can build the minified production bundle (lib/xmlbuilder2.min.js) after cloning the repository and issuingnpx webpack in your terminal. The bundle is also in the npm package, so you can also use a public npm CDN likejsDelivr orunpkg:
<!-- 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>
Please consider becoming a backer or sponsor to help support development.
About
An XML builder for node.js
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors12
Uh oh!
There was an error while loading.Please reload this page.
