Groovy解析xml

Groovy解析xml,xml,parsing,groovy,parsexml,Xml,Parsing,Groovy,Parsexml,我是groovy的新手 有人能帮助解析这个xml以获得每个num元素的值列表吗 NAMES> <NAMESet fetchSize="3"> <String StringNumber="1"> <NUM>1</NUM> </String> <String StringNumber="2"> <NUM>2</NUM> <

我是groovy的新手 有人能帮助解析这个xml以获得每个num元素的值列表吗

 NAMES>
    <NAMESet fetchSize="3">
    <String StringNumber="1">
        <NUM>1</NUM>
    </String>
    <String StringNumber="2">
        <NUM>2</NUM>
    </String>
    <String StringNumber="3">
        <NUM>3</NUM>
</NAMESet>
名称>
1.
2.
3.


提前谢谢

XML非常脆弱,但可以解决这个问题。它构建了一些HTML结构,不过:

xml=''' NAMES>
    <NAMESet fetchSize="3">
    <String StringNumber="1">
        <NUM>1</NUM>
    </String>
    <String StringNumber="2">
        <NUM>2</NUM>
    </String>
    <String StringNumber="3">
        <NUM>3</NUM>
</NAMESet>'''


names = new XmlSlurper( new org.cyberneko.html.parsers.SAXParser() ).parseText xml

assert names.BODY.NAMESET.STRING.NUM.collect { it.text() } == 
    ['1', '2', '3']
xml=''名称>
1.
2.
3.
'''
names=new-XmlSlurper(new-org.cyberneko.html.parsers.SAXParser()).parseText-xml
assert names.BODY.NAMESET.STRING.NUM.collect{it.text()}==
['1', '2', '3']

最后,我这样写道:

def records = new XmlParser().parseText(xml)
def size = records.ResultSet.Row.ID.size()
println(size)
def allRecords = records.ResultSet.Row.ID[1].text()
println(allRecords)
for (int i = 0; i < size; i++) {
println(records.ResultSet.Row.ID[i].text())
}
def records=new XmlParser().parseText(xml)
def size=records.ResultSet.Row.ID.size()
println(大小)
def allRecords=records.ResultSet.Row.ID[1].text()
println(所有记录)
对于(int i=0;i

无论如何,谢谢你。你试过这个吗?它是groovy和xml的官方文档。如果您对此有问题,请发布一些关于发生了什么的信息。而且,XML看起来很糟糕。这就是问题所在吗?给我们看看你的代码是的,我已经检查过了。我不知道如何获取NUM nodesPost代码的值,但它甚至与您在问题中发布的XML不一样是的,这里的想法是一样的