首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >一个XML解析器,允许以扁平化的方式处理单个标记的解析

一个XML解析器,允许以扁平化的方式处理单个标记的解析
EN

Stack Overflow用户
提问于 2021-10-12 00:47:43
回答 1查看 47关注 0票数 0

我正在寻找一个XML解析器,将允许以扁平化的方式处理单个标签解析。this模块所做的事情,但与服务器/node.js兼容。例如:

代码语言:javascript
代码运行次数:0
运行
复制
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>
`);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-12 09:26:56

我认为camaro会满足您的需求。您可以通过xpath模板重塑xml。

例如

代码语言:javascript
代码运行次数:0
运行
复制
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()
代码语言:javascript
代码运行次数:0
运行
复制
{
  "examples": [
    {
      "name": "simple",
      "items": [
        "one",
        "two",
        "three"
      ]
    }
  ]
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69533889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档