mirror of
https://github.com/vatesfr/xen-orchestra.git
synced 2026-09-10 22:14:48 -05:00
24 lines
591 B
JavaScript
24 lines
591 B
JavaScript
'use strict'
|
|
|
|
const sax = require('sax')
|
|
|
|
exports.parseXml = function parseXml(xml, { normalize = true, strict = true, trim = true } = {}) {
|
|
const stack = [{ children: [] }]
|
|
|
|
const parser = sax.parser(strict, { normalize, trim })
|
|
parser.ontext = text => {
|
|
stack[stack.length - 1].children.push(text)
|
|
}
|
|
parser.onopentag = ({ name, attributes }) => {
|
|
stack.push({ name, attributes, children: [] })
|
|
}
|
|
parser.onclosetag = () => {
|
|
const node = stack.pop()
|
|
stack[stack.length - 1].children.push(node)
|
|
}
|
|
|
|
parser.write(xml).close()
|
|
|
|
return stack[0].children[0]
|
|
}
|