Gathering detailed insights and metrics for xml
Gathering detailed insights and metrics for xml
Gathering detailed insights and metrics for xml
Gathering detailed insights and metrics for 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
Fast and simple Javascript-based XML generator/builder for Node projects.
npm install xml
Typescript
Module System
Node Version
NPM Version
99.9
Supply Chain
100
Quality
78
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
1,384,774,624
Last Day
320,782
Last Week
7,886,493
Last Month
35,152,788
Last Year
381,296,327
MIT License
282 Stars
60 Commits
51 Forks
10 Watchers
3 Branches
12 Contributors
Updated on Mar 24, 2025
Minified
Minified + Gzipped
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
Published on
Jan 31, 2016
Cumulative downloads
Total Downloads
1
Fast and simple Javascript-based XML generator/builder for Node projects.
$ npm install xml
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:
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>'
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 included use AVA. Use npm test
to run the tests.
$ npm test
There are examples in the examples directory.
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
license file detected
Details
Reason
0 existing vulnerabilities detected
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
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-05-26
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 MoreLast Day
-0%
320,782
Compared to previous day
Last Week
-9.4%
7,886,493
Compared to previous week
Last Month
-0%
35,152,788
Compared to previous month
Last Year
14.2%
381,296,327
Compared to previous year