gpt4 book ai didi

java - GPathResult ..存在或不存在节点

转载 作者:数据小太阳 更新时间:2023-10-29 01:58:05 24 4
gpt4 key购买 nike

我的 GPathResult 可以有 3 种方式之一的名称节点

1) 名称节点存在并具有值例如:约翰

2) 名称节点存在,但其中没有值。

3) 根本不存在名称节点。

在 Groovy 代码中,我如何使用我的 Gpathresult 区分上述 3 种情况。我是否使用类似 gPathResult.值()!=空?

伪代码:

if(name node is present and has a value){
do this
}

if(name node exists, but has no value in it){
do this
}

if( No name node exists at all){
do this
}

最佳答案

您必须测试 size()。继续使用 Olivier 的例子,只是修复了 GPathResult 并且它与 XmlSlurperXmlParser 一起工作,这里的代码:

def xml="<a><b>yes</b><c></c></a>"
def gpath = new XmlSlurper().parse(new ByteArrayInputStream(xml.getBytes()))
["b", "c", "d" ].each() {
println it
if (gpath[it].size()) {
println " exists"
println gpath[it].text() ? " has value" : " doesn't have a value"
} else {
println " does not exist"
}
}

关于java - GPathResult ..存在或不存在节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15958226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com