nooxml
v1.3.0
Published
Parse XML into predictable JavaScript objects and serialize it back.
Readme
noxml
Parse XML into predictable JavaScript objects and serialize it back.
Installation
npm install nooxmlRequires Node.js 20 or newer.
Usage
import {Builder, Parser} from "noxml";
const xml = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<foo>1</foo>
<bar>2</bar>
<foo bool="true">3</foo>
</xml>`;
const parser = new Parser();
const builder = new Builder();
const json = await parser.parse(xml);
json.xml.foo.push({"#": "4"});
console.log(builder.build(json));This will output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<foo>1</foo>
<bar>2</bar>
<foo bool="true">3</foo>
<foo>4</foo>
</xml>Data Shape
{
"xml": {
"@": 0,
"foo": [
{
"@": 1,
"_": {
"bool": "true"
},
"#": "3"
}
]
}
}@: element order index_: attributes#: text content- other keys: child elements
When inserting elements without @ into an array of parsed elements, the builder
infers their order from their array neighbours. At either end of an array, new
elements stay next to the existing element, inside the surrounding sibling
bounds. Arrays with no @ values retain their array order and follow indexed
siblings. Building does not modify the input object.
Development
npm install
npm test
npm run buildLicense
MIT
