【问题标题】:An XML parser that will allow handling individual tag parsing in a flattened way一个 XML 解析器,允许以扁平化方式处理单个标签解析
【发布时间】:2021-10-12 00:47:43
【问题描述】:

我正在寻找一种 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>
`);

【问题讨论】:

  • 更新了答案:)
  • 我已经回滚了你的编辑。提出问题,得到答案,然后改变该问题以提出其他问题是不合适的。如果您有新问题,请单击 按钮并将其作为新问题提出。此外,要求我们推荐或查找工具、软件库或其他场外资源的问题是题外话。如需更多信息,请参阅help center 指南。
  • 不确定这些政策是否对社区有所帮助。人们有不同的问题和需求。无论如何感谢您的审核!
  • 嗯,好的。 @SaidAkh 如果您需要更新的答案,您可以随时从这里查看修订版stackoverflow.com/revisions/69538092/3

标签: node.js xml xml-parsing


【解决方案1】:

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

【讨论】:

  • 谢谢你,@tuan-anh-tran!这个模块几乎完全符合我的要求。你能看看更新的问题吗?
  • @SaidAkh 更新了答案 :) 希望对您有所帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 2013-02-13
  • 1970-01-01
  • 2017-06-15
  • 1970-01-01
  • 2019-10-15
相关资源
最近更新 更多