@vates/xml
v2.0.0
Published
Simple XML formatting/parsing
Downloads
284
Maintainers
Keywords
Readme
@vates/xml
Simple XML formatting/parsing
Install
Installation of the npm package:
npm install --save @vates/xml
Usage
parseXml(xml, [opts])
Based on
sax
opts
is an optional object which can contain the following options:
normalize = true
: if true, then turn any whitespace into a single spacestrict = true
: whether or not to be a jerktrim = true
: whether or not to trim text nodes
import { parseXml } from '@xen-orchestra/xml/parse'
const tree = parseXml(`<?xml version="1.0" encoding="UTF-8"?>
<tag1 attr1="value1" attr2="value2">
Text & entities
<tag2 />
<tag2 />
<ns:tag3 />
</tag1>
`)
// → {
// name: 'tag1',
// attributes: { attr1: 'value1', attr2: 'value2' },
// children: [
// 'Text & entities',
// { name: 'tag2', attributes: {}, children: [] },
// { name: 'tag2', attributes: {}, children: [] },
// { name: 'ns:tag3', attributes: {}, children: [] }
// ]
// }
formatXml(tree, [opts])
opts
is an optional object which can contain the following options:
includeDeclaration = true
: whether to include an XML declarationindent = 2
: string or number of spaces to use to indent; if an empty string or0
, no indent or new lines will be used
import { formatXml } from '@xen-orchestra/xml/format'
formatXml({
name: 'tag1',
attributes: { attr1: 'value1', attr2: 'value2' },
children: ['Text & entities', { name: 'tag2' }, { name: 'tag2' }, { name: 'ns:tag3' }],
})
// → <?xml version="1.0" encoding="UTF-8"?>
// <tag1 attr1="value1" attr2="value2">
// Text & entities
// <tag2 />
// <tag2 />
// <ns:tag3 />
// </tag1>
Contributions
Contributions are very welcomed, either on the documentation or on the code.
You may:
- report any issue you've encountered;
- fork and create a pull request.