Convert XML to JS object asynchronously and maintain the order.
npm install xml2jsobj
require("xml2jsobj").convert(
'<r>\
<a>A1</a>\
<b c="d">B</b>\
<a>\
A2\
<e />\
</a>\
</r>',
function(err, result) {
console.dir(result);
}
);
{ "name": "R",
"children": [
{ "name": "A",
"children": ["A1"]
},
{ "name": "B",
"attributes": {"C": "d"},
"children": ["B"]
},
{ "name": "A",
"children": [
"A2",
{"name": "E"}
]
}
]
}
- There are at most three member for each node:
name
,attributes
,children
. - Maintain the order of the children. In the example above, the
<a>
tags are NOT combined as an array (which most other packages such asxml2js
and its dependents do), and therefore the result object still knows that the second<a>
tag is after the<b>
tag. - Even with only one child, the node would still have a
children
member as an array.
Call <obj>.init
for initialization if you need. The arguments are just like that for sax's parser
, except that trim
is default to true
.