3

属性名を使用して属性値のリストを取得する GPath 式を探します。

def xmltxt = """<reports>
    <report category="weather">sunny</report>
    <report category="sports">golf</report>
    <report category="business">
      <subreport category="deals">wallstreet</subreport>
    </report>
    <report>NA</report>
    <report category="life">gossip</report>
 </reports>"""

..すべての属性を検索するとき、属性がドキュメント内categoryのどこに存在するかに関係なく、これを取得したい:category

[weather, sports, business, deals, life]

category...しかし、私の試みはすべて、私が望む以上のものを取得します。属性を持たないノードを返しているようです。リストから空の要素を削除できますが、なぜこれが起こっているのか知りたいです。

[, weather, sports, business, deals, , life]

  def names = xml.'**'.findAll{
     it.@category        
  }.collect{
     it.@category
  }
4

1 に答える 1

4
def parsed = new XmlParser().parseText( xmltxt )
parsed.'**'*.attribute( 'category' ).findAll()

すべきです。

ここに、XmlSlurper解決策があります:

def parsed = new XmlSlurper().parseText( xmltxt )
parsed.'**'*.attributes().findResults { it.category }
于 2014-06-01T02:22:50.407 に答える