简体   繁体   English

如何用 groovy 迭代 xml 个节点

[英]how to iterate xml nodes with groovy

I'm trying to iterate through an xml file with groovy to get some values.我正在尝试使用 groovy 遍历 xml 文件以获取一些值。 I found many people with the same problem, but the solution they used doesn't work for me, or it's too complicated.我发现很多人有同样的问题,但他们使用的解决方案对我不起作用,或者太复杂了。 I'm not a groovy dev, so I need a bullet proof solution which I can implement.我不是 groovy 开发人员,所以我需要一个我可以实施的防弹解决方案。

Basically I have an xml response file that looks like this: ( it looks bad but that's what I get)基本上我有一个 xml 响应文件,看起来像这样:(它看起来很糟糕,但这就是我得到的)

<Body>
 <head>
  <Details>

   <items>
    <item>
     <AttrName>City</AttrName>
     <AttrValue>Rome</AttrValue>
    </item>

    <item>
     <AttrName>Street</AttrName>
     <AttrValue>Via_del_Corso</AttrValue>
    </item>

    <item>
     <AttrName>Number</AttrName>
     <AttrValue>34</AttrValue>
    </item>

   </items>
 
  </Details>
 </head>
</Body>

I've already tried this solution I found here on StackOverflow to print the values:我已经尝试过在 StackOverflow 上找到的这个解决方案来打印值:

def envelope = new XmlSlurper().parseText("the xml above")

envelope.Body.head.Details.items.item.each(item -> println( "${tag.name}")  item.children().each {tag -> println( "  ${tag.name()}: ${tag.text()}")} }  

the best I get is我得到的最好的是

ConsoleScript11$_run_closure1$_closure2@2bfec433
ConsoleScript11$_run_closure1$_closure2@70eb8de3
ConsoleScript11$_run_closure1$_closure2@7c0da10
Result: CityRomeStreetVia_del_CorsoNumber34

I can also remove everything after the first println, and anything inside it, the result is the same我也可以删除第一个 println 之后的所有内容,以及其中的任何内容,结果是一样的

My main goal here is not to print the values but to extrapolate those values from the xml and save them as string variables... I know that using strings is not the best practice but I just need to understand now.我在这里的主要目标不是打印值,而是从 xml 推断这些值并将它们保存为字符串变量......我知道使用字符串不是最佳实践,但我现在只需要了解。

Your code as is had 2 flaws:您的代码有两个缺陷:

  1. with envelope.Body you would NOT find anythingenvelope.Body 。身体你找不到任何东西
  2. if you fix No. 1, you would run into multiple compile errors for each(item -> println( "${tag.name}") . Here the ( is used instead of { and you use an undefined tag variable here.如果你修复第一个,你会遇到多个编译错误each(item -> println( "${tag.name}") 。这里使用(而不是{并且你在这里使用未定义的tag变量。

The working code would look like:工作代码如下所示:

import groovy.xml.*

def xmlBody = new XmlSlurper().parseText '''\
<Body>
 <head>
  <Details>

   <items>
    <item>
     <AttrName>City</AttrName>
     <AttrValue>Rome</AttrValue>
    </item>

    <item>
     <AttrName>Street</AttrName>
     <AttrValue>Via_del_Corso</AttrValue>
    </item>

    <item>
     <AttrName>Number</AttrName>
     <AttrValue>34</AttrValue>
    </item>

   </items>
 
  </Details>
 </head>
</Body>'''

xmlBody.head.Details.items.item.children().each {tag ->
  println( "  ${tag.name()}: ${tag.text()}")
}

and print:并打印:

  AttrName: City
  AttrValue: Rome
  AttrName: Street
  AttrValue: Via_del_Corso
  AttrName: Number
  AttrValue: 34

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM