Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An XML builder for node.js

License

NotificationsYou must be signed in to change notification settings

oozcitak/xmlbuilder2

Repository files navigation

An XML builder fornode.js.

GitHub LicenseNPM VersionNPM DownloadsjsDelivr hits (npm)

Node.js CICode Coverage

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"?><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>

Usage in the browser:

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>

Donations:

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

Donate Button

About

An XML builder for node.js

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

    Packages

    No packages published

    Contributors12


    [8]ページ先頭

    ©2009-2025 Movatter.jp