Groovy의 속성에 대한 특정 값이있는 노드의 텍스트는 어떻게 찾을 수 있습니까?
-
14-12-2019 - |
문제
xmlslurper를 사용하고 있습니다.내 코드는 아래에 있습니다 (그러나 작동하지 않습니다).문제는 속성 "ID"가없는 노드가 히트가 발생하면 실패합니다.이를 어떻게 설명합니까?
//Parse XML
def page = new XmlSlurper(false,false).parseText(xml)
//Now save the value of the proper node to a property (this fails)
properties[ "finalValue" ] = page.find {
it.attributes().find { it.key.equalsIgnoreCase( 'id' ) }.value == "myNode"
};
.
ID "속성이없는 노드를 고려하면 실패하지 않습니다.어떻게해야합니까?
해결책 2
가정적으로 나는 단순히 depthfirst를 사용하면 일할 수 있습니다.그래서 :
properties[ "finalValue" ] = page.depthFirst().find {
it.attributes().find { it.key.equalsIgnoreCase( 'id' ) }.value == "myNode"
};
. 다른 팁
GPath 표기법을 사용하여 "@Id"가 비어 있는지 확인할 수 있습니다.
다음 코드 스 니펫은 마지막 요소를 찾습니다 (id 속성은 "b"이므로 값이 "brizz"이기 때문에 "brizz"와 "b"를 인쇄합니다.
def xml = new XmlSlurper().parseText("<foo><bar>bizz</bar><bar id='A'>bazz</bar><bar id='B'>bizz</bar></foo>")
def x = xml.children().find{!it.@id.isEmpty() && it.text()=="bizz"}
println x
println x.@id
. 제휴하지 않습니다 StackOverflow