如何强制 xml2js 为某些键输出数组?

我有一些 XML,我希望它输出到一个数组中。但是当只有一项存在时,xml2js 会将一项输出到一个对象。当有多个项目时,它输出为一个数组。为了一致性,我如何强制它成为一个数组?


我的 XML 看起来像这样:


const xml = `<Shift>

    <Transaction>

      <Currency>GBP</Currency>

      <Amount>24.50</Amount>

    </Transaction>

</Shift>`


parseStringPromise(xml).then(json => console.log(JSON.stringify(json)))

您可以看到输出为:


{

  "Transaction": {

    "Currency": "GBP",

    "Amount": "24.50"

  }

}

但通常有多个事务。因此,通常 XML 将如下所示:


const xml = `<Shift>

    <Transaction>

      <Currency>GBP</Currency>

      <Amount>24.50</Amount>

    </Transaction>

    <Transaction>

      <Currency>GBP</Currency>

      <Amount>25.50</Amount>

    </Transaction>

    <Transaction>

      <Currency>GBP</Currency>

      <Amount>26.50</Amount>

    </Transaction>

    <Transaction>

      <Currency>GBP</Currency>

      <Amount>27.50</Amount>

    </Transaction>

</Shift>`


parseStringPromise(xml).then(json => console.log(JSON.stringify(json)))

它像这样输出到一个数组中


{

  "Transaction": [

    {

      "Currency": "GBP",

      "Amount": "24.50"

    },

    {

      "Currency": "GBP",

      "Amount": "25.50"

    },

    {

      "Currency": "GBP",

      "Amount": "26.50"

    },

    {

      "Currency": "GBP",

      "Amount": "27.50"

    }

  ]

}

我怎样才能确保即使Transactionxml 中只有 1 个项目,它也会输出到一个数组?


是否有需要传递给解析器的选项,或者是否需要重新格式化我的 XML?


UYOU
浏览 197回答 2
2回答

茅侃侃

为此,您可以使用camaro。camaro 允许您编写一个基于 xpath 的模板来指定您想要的输出方式。在这种情况下,你指定你想要一个数组const { transform } = require('camaro')const xml = `<Shift>&nbsp; &nbsp; <Transaction>&nbsp; &nbsp; &nbsp; <Currency>GBP</Currency>&nbsp; &nbsp; &nbsp; <Amount>24.50</Amount>&nbsp; &nbsp; </Transaction>&nbsp; &nbsp; <Transaction>&nbsp; &nbsp; &nbsp; <Currency>GBP</Currency>&nbsp; &nbsp; &nbsp; <Amount>25.50</Amount>&nbsp; &nbsp; </Transaction>&nbsp; &nbsp; <Transaction>&nbsp; &nbsp; &nbsp; <Currency>GBP</Currency>&nbsp; &nbsp; &nbsp; <Amount>26.50</Amount>&nbsp; &nbsp; </Transaction>&nbsp; &nbsp; <Transaction>&nbsp; &nbsp; &nbsp; <Currency>GBP</Currency>&nbsp; &nbsp; &nbsp; <Amount>27.50</Amount>&nbsp; &nbsp; </Transaction></Shift>`;(async function () {&nbsp; &nbsp; const template = {&nbsp; &nbsp; &nbsp; &nbsp; transactions: ['/Shift/Transaction', {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Currency: 'Currency',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Amount: 'Amount'&nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; }&nbsp; &nbsp; console.log(await transform(xml, template));})()输出{ transactions:&nbsp; &nbsp;[ { Currency: 'GBP', Amount: '24.50' },&nbsp; &nbsp; &nbsp;{ Currency: 'GBP', Amount: '25.50' },&nbsp; &nbsp; &nbsp;{ Currency: 'GBP', Amount: '26.50' },&nbsp; &nbsp; &nbsp;{ Currency: 'GBP', Amount: '27.50' } ] }即使只有 1 个元素const { transform } = require('../')const xml = `<Shift>&nbsp; &nbsp; <Transaction>&nbsp; &nbsp; &nbsp; <Currency>GBP</Currency>&nbsp; &nbsp; &nbsp; <Amount>24.50</Amount>&nbsp; &nbsp; </Transaction></Shift>`;(async function () {&nbsp; &nbsp; const template = {&nbsp; &nbsp; &nbsp; &nbsp; transactions: ['/Shift/Transaction', {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Currency: 'Currency',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Amount: 'Amount'&nbsp; &nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; }&nbsp; &nbsp; console.log(await transform(xml, template));})()输出{ transactions: [ { Currency: 'GBP', Amount: '24.50' } ] }

猛跑小猪

有很多 XML 到 JSON 转换器,它们都有这种问题。它们在某些情况下会做您想做的事,而在其他情况下则不会,而且它们都没有提供太多控制权。如果您想控制生成的 JSON,我建议使用 XSLT 3.0:这允许您在调用xml-to-json()进行转换之前将 XML 转换为与您想要生成的 JSON 的结构相对应。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript