0
XML에서 하위 노드를 제거하려고합니다. 내 스크립트는 작동하지만 .. 자식 노드를 제거하지 않고 제거하려는 노드뿐 아니라.XML에서 하위 노드 삭제
import groovy.xml.*;
def employees='''<Employees>
<Employee>
<ID>123</ID>
<Name>xyz</Name>
<Addresses>
<Address>
<Country>USA</Country>
<ZipCode>40640</ZipCode>
</Address>
</Addresses>
</Employee>
<Employee>
<ID>345</ID>
<Name>abc</Name>
<Addresses>
<Address>
<Country>CA</Country>
<ZipCode>50640</ZipCode>
</Address>
</Addresses>
</Employee>
</Employees>'''
def fields = ['Name','ZipCode']
def xml = new XmlParser().parseText(employees)
xml.Employee.each { node ->
node.children().reverse().each{
if(!fields.contains(it.name())) {
node.remove(it)
}
}
}
XmlUtil.serialize(xml)
xml의 각 직원으로부터 노드 ZipCode를 삭제하는 방법은 무엇입니까?
감사합니다. 이것은 내가 기대했던 방식으로 작동합니다 !! –
내 응용 프로그램이 메서드의 서명을 throw하지 않기 때문에 replaceNode 대신 remove 메서드를 사용하는 방법이 있습니까 : groovy.util.Node.replaceNode() 및 종속성을 추가 할 수 없습니다! –