考虑下面的示例xml。
<Groups>
<Group id="01" check="true">
<name>Value</name>
<age>test</age>
<setData>
<data type="module">module</data>
<data enabled="true">true</data>
</setData>
</Group>
<Group id="02" check="true">
<name>Value</name>
<age>test</age>
<setData>
<data type="module">module</data>
<data enabled="true">true</data>
</setData>
</Group>
</Groups>
我想使用路径表达式获取在setData中为GroupId = 1启用的值。
下面是我的尝试。这是针对此问题的临时解决方案,但会在数组中查找位置。
String groupId = "1"; //to make group id dynamic.
String pathExpression = "Groups/group[@id='"+groupId+"']/setData/data[2]/text()"
取而代之的是,我想通过给属性名称“启用”来获取值,以便它将检查名称而不是位置。
最佳答案
您已经在使用正确的语法来通过属性值获取group
,因此也只需对data
元素执行类似的操作,除了不需要检查属性值,只需检查它是否存在...。 。
String pathExpression = "Groups/group[@id='"+groupId+"']/setData/data[@enabled]/text()"
关于xml - 如何使用路径表达式从数组中获取属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51823750/