在Groovy中将XML名称空间与XmlSlurper一起使用-如何正确查询路径?

问题描述 投票:1回答:1

我有以下示例XML:

<root>

<table xmlns:h="http://www.w3.org/TR/html4/">
  <tr>
    <td>Apples</td>
    <td>Bananas</td>
  </tr>
</table>

<table xmlns:f="https://www.w3schools.com/furniture">
  <name>African Coffee Table</name>
  <width>80</width>
  <length>120</length>
</table>

</root>
def slurper = new XmlSlurper().parseText(someXMLText)
def hNs = new groovy.xml.Namespace(
                    "http://www.w3.org/TR/html4/", 'h')
def fNs = new groovy.xml.Namespace(
                    "https://www.w3schools.com/furniture", 'h')
println slurper.root[hNs.table].tr.td //not giving any response

因为有两个表标签具有不同的标签。如何使用名称空间使用gpath在标签下获取Apples值。

groovy xml-parsing xml-namespaces
1个回答
0
投票
<table xmlns="http://www.w3.org/TR/html4/"> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table>
© www.soinside.com 2019 - 2024. All rights reserved.