Parse an XML file to JSON object, modify text nodes and write it to a stream
I found myself needing to update text nodes in a maven pom from a node project
Given a file that looks like this
<this>
<is>
<another>
XML file
</another>
</is>
</this>
const path = require('path');
const xmlEdit = require('xml-edit');
const editor = xmlEdit.getInstance({
indentation: 2,
strict: true,
});
const xmlObject = await editor.read(path.resolve('test.xml'));
/*
{
"this": {
"is": {
"another": "XML file"
}
}
}
*/
xmlObject.this.is.another = 'ohai';
await editor.write(path.resolve('test.xml'), xmlObject, process.stdout);
opts.indentation: number of spaces to use when writing
(default: 2
)
opts.strict: parse XML strictly or not
(default: true
)
filePath: Path to the file (doh)
filePath: Path to the original file
xmlObject: Specification of which text nodes to change
stream: A writeable stream that the result gets sent to
- Original indentation in the file is not respected
- You can't edit attributes, in fact they don't even get returned by
read
- You can't edit tagnames
- Your attributes will be inlined in the tag in the result
This has not been tested very thoroughly so there are probably bugs. Let me know and I'll look at it.
For now consider this a beta at best.
Don't use it then :)
Or file a bug and/or open a PR.