首页 > 解决方案 > 使用命令行或使用 shell 脚本将 xml 转换为 json

问题描述

<?xml version="1.0" encoding="utf-8"??>
<resources>
  <data id="V701">
    <string name="MSG_V701_ID">V701</string>
    <string name="MSG_V701_TITLE">abc</string>
    <string name="MSG_V701_BODY">This title is currently unable</string>
  </data>
  <data id="V702">
    <string name="MSG_V702_ID">V702</string>
    <string name="MSG_V702_TITLE">Play</string>
    <string name="MSG_V702_BODY">This title is currently unable to play</string>
  </data>
</resources>

如何在 ubuntu 或 shell 脚本中使用该命令行来执行此操作

标签: jsonxmlshellxml-parsingsh

解决方案


没有本地 Bash 工具可以做到这一点。所以你必须使用像这样的专用工具。

你没有解释你希望 JSON 的样子,所以是这样的......

xidel -s input.xml -e '{|//data/{@* :{|string/{@* :text()}|}}|}'
{
  "V701": {
    "MSG_V701_ID": "V701",
    "MSG_V701_TITLE": "abc",
    "MSG_V701_BODY": "This title is currently unable"
  },
  "V702": {
    "MSG_V702_ID": "V702",
    "MSG_V702_TITLE": "Play",
    "MSG_V702_BODY": "This title is currently unable to play"
  }
}

...你在找什么?


推荐阅读