XML 允许以扁平化方式处理单个标签解析的解析器

An XML parser that will allow handling individual tag parsing in a flattened way

我正在寻找一个 XML 允许以扁平化方式处理单个标签解析的解析器。 this 模块做的事情,但 server/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>
`);

我认为 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"
      ]
    }
  ]
}