xmltwojs
v4.0.0
Published
Simple XML to JavaScript object converter.
Downloads
1
Readme
xmlTwoJs
A super simple parser for converting xml to json. Roughly 2x as fast as xml2js.
Installation
Simplest way to install xml2js
is to use npm, just npm install xmltwojs
.
Example
const xmltwojs = require('xmltwojs');
const json = xmltwojs.parse('<root attr="7">Hello xmltwojs!</root>');
console.log(json);
Which outputs the following:
{
"root": {
"attr": "7",
"_": "Hello xmltwojs!"
}
}
A few important notes.
- The inner text of a node will always be names "_" in the resulting json object
- No type conversion is attempted. For example, in the case above the value of "attr" is a string, not a number
- xmlTwoJs will automatically create an array when it finds multiple sibling nodes with the same name. However, because of the nature of xml it is not possible to detect arrays with only one value.
Consider the following example:
const xml = `<root>
<array1>
<item>1</item>
<item>2</item>
</array1>
<array2>
<item>3</item>
</array2>
</root>`;
const json = xmltwojs.parse(xml);
console.log(json);
which yields
{
"root": {
"array1": {
"item": [
{
"_": "1"
},
{
"_": "2"
}
]
},
"array2": {
"item": {
"_": "3"
}
}
}
}
Note that array1.item
is an array. But, since "array2" only has one "item" node, array2.item
is an object.
Testing
npm i
npm test