xml - groovy xmlSlurper 检查同一级别内的更多字段
问题描述
我有这个xml。
<?xml version="1.0" encoding="utf-8"?>
<entry>
<id>E0000</id>
<link href="href">
<inline>
<entry>
<link href="href">
<inline>
<feed>
<entry>
<id>E0001</id>
<content type="application/xml">
<props>
<status/>
</props>
</content>
</entry>
<entry>
<id>E0002</id>
<content type="application/xml">
<props>
<status/>
</props>
</content>
</entry>
<entry>
<id>E0003</id>
<content type="application/xml">
<props>
<status>S00</status>
</props>
</content>
</entry>
</feed>
</inline>
</link>
</entry>
</inline>
</link>
</entry>
我正在使用 xmlSlurper 检查最深的“条目”标签中是否有一个同时具有“id”=“E0001”和“status”=“S00”或“id”=“E0002”和“status”=“S00” ”。像这样:(id=E0001 AND status=S00) OR (id=E0002 AND status=S00)。
我正在使用此代码(我正在使用 Groovy Web 控制台对其进行测试)。
def text = '<?xml version=\"1.0\" encoding=\"utf-8\"?><entry><id>E0000</id><link href=\"href\"><inline><entry><link href=\"href\"><inline><feed><entry><id>E0001</id><content type=\"application/xml\"><props><status>S00</status></props></content></entry><entry><id>E0002</id><content type=\"application/xml\"><props><status/></props></content></entry><entry><id>E0003</id><content type=\"application/xml\"><props><status/></props></content></entry></feed></inline></link></entry></inline></link></entry>'
def response = new XmlSlurper().parseText(text)
def result = (response.link.inline.entry.link.inline.feed.find {(it.entry.content.props.status.text() == 'S00' & it.entry.id.text().contains('E0001')) | ((it.entry.content.props.status.text() == 'S00' & it.entry.id.text().contains('E0002')))}).size() > 0 ? 'true' : 'false'
println(result)
但是,即使 status=S00 位于 id=E0003 的“条目”标签下,结果也是如此,这是不需要的。我怎样才能调整我上面的代码?
解决方案
感谢克弗里克的建议。我已经对其进行了调整,并且可以正常工作。
def result = (response.link.inline.entry.link.inline.feed.entry.find {(it.content.props.status.text() == 'S00' & it.id.text().contains('E0001')) | ((it.content.props.status.text() == 'S00' & it.id.text().contains('E0002')))}).size() > 0 ? 'true' : 'false'
推荐阅读
- java - 在 Intellij 中打开 Javafx 模块到 Gson 模块
- mongodb - 如何使用聚合管道处理大型集合并无限制地返回所有结果?
- spring - 如何在 Spring MVC 前端监控服务器端事件?
- python - AssertionError:视图函数映射正在覆盖现有端点函数:包装器
- php - 从 PHP 中的单个循环回显到分离的 html 部分
- django - Django rest 框架 + 简单 JWT - 始终启用权限类
- c# - 向 blazor 中的表格添加 onclick 事件,表格由 for 循环创建
- javascript - 状态未定义 no - 反应类组件中的 undef
- angular - 如何将时区附加到 Datepipe 的转换函数?
- mysql - 如果计划 1 年,每天取 3 行