我正在寻找一个XML解析器,将允许以扁平化的方式处理单个标签解析。this模块所做的事情,但与服务器/node.js兼容。例如:
const xmlToReact = new someXmlParser({
Example: (attrs) => {console.log("get all necessary info")},
Item: (attrs) => {console.log("get all necessary info")}
});
const reactTree = someXmlParser.convert(`
<Example name="simple">
<Item i="1">one</Item>
<Item>two</Item>
<Item>three</Item>
</Example>
`);
发布于 2021-10-12 01:26:56
我认为camaro会满足您的需求。您可以通过xpath模板重塑xml。
例如
const { transform } = require('camaro')
const xml = `
<Example name="simple">
<Item i="1">one</Item>
<Item>two</Item>
<Item>three</Item>
</Example>
`
const template = {
examples: ['/Example', {
name: '@name',
items: ['Item', '.']
}]
}
async function main() {
const output = await transform(xml, template)
console.log(JSON.stringify(output, null, 2))
}
main()
{
"examples": [
{
"name": "simple",
"items": [
"one",
"two",
"three"
]
}
]
}
https://stackoverflow.com/questions/69533889
复制