xml
Fast and simple Javascript-based XML generator/builder for Node projects.
Install
$ npm install xml
API
xml(xmlObject, options)
Returns a XML
string.
var xml = ;var xmlString = ;
xmlObject
xmlObject
is a normal JavaScript Object/JSON object that defines the data for the XML string.
Keys will become tag names.
Values can be an array of xmlObjects
or a value such as a string
or number
.
=== '<a>1</a>' === '<nested><keys><fun>hi</fun></keys></nested>'
There are two special keys:
_attr
Set attributes using a hash of key/value pairs.
=== '<a attributes="are fun" too="!">1</a>'
_cdata
Value of _cdata
is wrapped in xml ![CDATA[]]
so the data does not need to be escaped.
=== '<a><![CDATA[i\'m not escaped: <xml>!]]></a>'
Mixed together:
=== '<a attr="hi"><![CDATA[I\'m not escaped]]></a>'
options
indent
optional string What to use as a tab. Defaults to no tabs (compressed).
For example you can use '\t'
for tab character, or ' '
for two-space tabs.
stream
Return the result as a stream
.
Stream Example
var elem = xml;var stream = ;stream;elem;elem;elem;elem; /*result:data: <toys decade="80s" locale="US">data: <toy>Transformers</toy>data: <toy>GI Joe</toy>data: <toy> <name>He-man</name> </toy>data: </toys>*/
Declaration
optional Add default xml declaration as first node.
options are:
- encoding: 'value'
- standalone: 'value'
Declaration Example
//result: '<?xml version="1.0" encoding="UTF-8"?><a>test</a>' //result: '<?xml version="1.0" encoding="UTF-16" standalone="yes"?><a>test</a>'
Examples
Simple Example
var example1 = url: 'http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' ;console;//<url>http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower</url>
Example with attributes
var example2 = url: _attr: hostname: 'www.google.com' path: '/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' ;console;//result: <url hostname="www.google.com" path="/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower"/>
Example with array of same-named elements and nice formatting
var example3 = toys: toy: 'Transformers' toy: 'GI Joe' toy: 'He-man' ;console;//result: <toys><toy>Transformers</toy><toy>GI Joe</toy><toy>He-man</toy></toys>console;/*result:<toys> <toy>Transformers</toy> <toy>GI Joe</toy> <toy>He-man</toy></toys>*/
More complex example
var example4 = toys: _attr: decade: '80s' locale: 'US' toy: 'Transformers' toy: 'GI Joe' toy: 'He-man' ;console;/*result:<toys decade="80s" locale="US"> <toy>Transformers</toy> <toy>GI Joe</toy> <toy>He-man</toy></toys>*/
Even more complex example
var example5 = toys: _attr: decade: '80s' locale: 'US' toy: 'Transformers' toy: _attr: knowing: 'half the battle' 'GI Joe' toy: name: 'He-man' description: _cdata: '<strong>Master of the Universe!</strong>' ;console;/*result:<toys decade="80s" locale="US"> <toy>Transformers</toy> <toy knowing="half the battle"> GI Joe </toy> <toy> <name>He-man</name> <description><![CDATA[<strong>Master of the Universe!</strong>]]></description> </toy></toys>*/
Tests
Tests included use AVA. Use npm test
to run the tests.
$ npm test
Examples
There are examples in the examples directory.
Contributing
Contributions to the project are welcome. Feel free to fork and improve. I accept pull requests when tests are included.