Fast and simple Javascript-based XML generator/builder for Node projects.
Installations
npm install xml
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
4.2.1
NPM Version
2.14.16
Score
100
Supply Chain
100
Quality
78
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
Download Statistics
Total Downloads
1,211,169,245
Last Day
324,880
Last Week
4,638,963
Last Month
29,498,483
Last Year
354,297,127
GitHub Statistics
276 Stars
60 Commits
51 Forks
10 Watching
2 Branches
12 Contributors
Package Meta Information
Latest Version
1.0.1
Package Id
xml@1.0.1
Size
6.65 kB
NPM Version
2.14.16
Node Version
4.2.1
Publised On
31 Jan 2016
Total Downloads
Cumulative downloads
Total Downloads
1,211,169,245
Last day
-76.5%
324,880
Compared to previous day
Last week
-36.5%
4,638,963
Compared to previous week
Last month
-8.9%
29,498,483
Compared to previous month
Last year
14.2%
354,297,127
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
1
xml
Fast and simple Javascript-based XML generator/builder for Node projects.
Install
$ npm install xml
API
xml(xmlObject, options)
Returns a XML
string.
1var xml = require('xml'); 2var xmlString = xml(xmlObject, options);
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
.
1xml({a: 1}) === '<a>1</a>' 2xml({nested: [{ keys: [{ fun: 'hi' }]}]}) === '<nested><keys><fun>hi</fun></keys></nested>'
There are two special keys:
_attr
Set attributes using a hash of key/value pairs.
1xml({a: [{ _attr: { attributes: 'are fun', too: '!' }}, 1]}) === '<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.
1xml({a: { _cdata: "i'm not escaped: <xml>!"}}) === '<a><![CDATA[i\'m not escaped: <xml>!]]></a>'
Mixed together:
1xml({a: { _attr: { attr:'hi'}, _cdata: "I'm not escaped" }}) === '<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
1var elem = xml.element({ _attr: { decade: '80s', locale: 'US'} }); 2var stream = xml({ toys: elem }, { stream: true }); 3stream.on('data', function (chunk) {console.log("data:", chunk)}); 4elem.push({ toy: 'Transformers' }); 5elem.push({ toy: 'GI Joe' }); 6elem.push({ toy: [{name:'He-man'}] }); 7elem.close(); 8 9/* 10result: 11data: <toys decade="80s" locale="US"> 12data: <toy>Transformers</toy> 13data: <toy>GI Joe</toy> 14data: <toy> 15 <name>He-man</name> 16 </toy> 17data: </toys> 18*/
Declaration
optional Add default xml declaration as first node.
options are:
- encoding: 'value'
- standalone: 'value'
Declaration Example
1xml([ { a: 'test' }], { declaration: true }) 2//result: '<?xml version="1.0" encoding="UTF-8"?><a>test</a>' 3 4xml([ { a: 'test' }], { declaration: { standalone: 'yes', encoding: 'UTF-16' }}) 5//result: '<?xml version="1.0" encoding="UTF-16" standalone="yes"?><a>test</a>'
Examples
Simple Example
1var example1 = [ { url: 'http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' } ]; 2console.log(XML(example1)); 3//<url>http://www.google.com/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower</url>
Example with attributes
1var example2 = [ { url: { _attr: { hostname: 'www.google.com', path: '/search?aq=f&sourceid=chrome&ie=UTF-8&q=opower' } } } ]; 2console.log(XML(example2)); 3//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
1var example3 = [ { toys: [ { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ]; 2console.log(XML(example3)); 3//result: <toys><toy>Transformers</toy><toy>GI Joe</toy><toy>He-man</toy></toys> 4console.log(XML(example3, true)); 5/* 6result: 7<toys> 8 <toy>Transformers</toy> 9 <toy>GI Joe</toy> 10 <toy>He-man</toy> 11</toys> 12*/
More complex example
1var example4 = [ { toys: [ { _attr: { decade: '80s', locale: 'US'} }, { toy: 'Transformers' } , { toy: 'GI Joe' }, { toy: 'He-man' } ] } ]; 2console.log(XML(example4, true)); 3/* 4result: 5<toys decade="80s" locale="US"> 6 <toy>Transformers</toy> 7 <toy>GI Joe</toy> 8 <toy>He-man</toy> 9</toys> 10*/
Even more complex example
1var 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>'} } ] } ] } ]; 2console.log(XML(example5, true)); 3/* 4result: 5<toys decade="80s" locale="US"> 6 <toy>Transformers</toy> 7 <toy knowing="half the battle"> 8 GI Joe 9 </toy> 10 <toy> 11 <name>He-man</name> 12 <description><![CDATA[<strong>Master of the Universe!</strong>]]></description> 13 </toy> 14</toys> 15*/
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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 5/27 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 8 are checked with a SAST tool
Score
3.2
/10
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to xml
xml-name-validator
Validates whether a string matches the production for an XML name or qualified name
fast-xml-parser
Validate XML, Parse XML, Build XML without C/C++ based libraries
entities
Encode & decode XML and HTML entities with ease & speed
moddle-xml
XML import/export for documents described with moddle