【问题标题】:Parsing just the next sublevel category in an xml file [duplicate]仅解析 xml 文件中的下一个子级别类别 [重复]
【发布时间】:2016-07-12 14:22:08
【问题描述】:

我有一个名为 test.xml 的 xml 文件。

<deathList>
<victimGroupA>
  <person1>
    <name>The Pope</name>
    <email>pope@vatican.gob.va</email>
    <is_satan>0</is_satan>
  </person1>
  <person2>
    <name>George Bush</name>
    <email>father@nwo.com</email>
    <is_satan>1</is_satan>
   </person2>
  <person3>
    <name>George Bush Jr</name>
    <email>son@nwo.com</email>
    <is_satan>0</is_satan>
  </person3>
</victimGroupA>
<victimGroupB>
  <person1>
    <name>bob</name>
    <email>bob@bobby.com</email>
    <is_satan>0</is_satan>
  </person1>
  <person2>
    <name>mike</name>
    <email>mike@michael.com</email>
    <is_satan>0</is_satan>
  </person2>
</victimGroupB>
<victimGroupC>
  <person1>
    <name>kenny</name>
    <email>kenny@youkilledkenny.com</email>
    <is_satan>0</is_satan>
  </person1>
</victimGroupC>
</deathList>

我想编写一个 bashrc 脚本来尝试回显以下内容:

victimGroupA
person1
person2
person3
victimGroupB
person1
person2
victimGroupC
person1

基本上它应该检测所有的victimGroup* 并打印出它的所有下一个子级别类别。最简单的方法是什么?我需要它与 centos 和 ubuntu 兼容。

【问题讨论】:

  • 我只是将他的一些 xml 值用于我希望运行的测试。但是我仍然不知道如何抓取xml的“子类别”。
  • 你必须在shell脚本中做还是你愿意写一些代码......
  • 一旦你对 XPath 有了一个合理的理解,这绝对不再是一个bash 的问题,并且尝试使用正则表达式或其他 shell 脚本方法来解析 XML 只是方向错误,正如大量演示过的那样之前。

标签: xml linux bash shell xml-parsing


【解决方案1】:

你可以试试这个;

egrep 'victim|person' test.xml | grep -v / | sed -e 's/\(\)\|\(\) //g'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    相关资源
    最近更新 更多