 |
Subscribe to this site |
|
XmlSlurper with Namespaces

Published 1 decade ago
by
mrhaki
with tags
xml
import groovy.xml.*
def xml = '''
<books xmlns:meta="http://meta/book/info" count="3">
<book id="1">
<title lang="en">Groovy in Action</title>
<meta:isbn>1-932394-84-2</meta:isbn>
</book>
<book id="2">
<title lang="en">Groovy Programming</title>
<meta:isbn>0123725070</meta:isbn>
</book>
<book id="3">
<title>Groovy & Grails</title>
<!--Not yet available.-->
</book>
<book id="4">
<title>Griffon Guide</title>
</book>
</books>
'''
def books = new XmlSlurper().parseText(xml).declareNamespace([meta:'http://meta/book/info'])
assert books instanceof groovy.util.slurpersupport.GPathResult
assert 4 == books.book.size()
assert 11 == books.breadthFirst().size()
assert 'Groovy in Action' == books.book[0].title.toString()
assert 'Groovy Programming' == books.book.find { it.@id == '2' }.title as String
assert [1, 2, 3] == books.book.findAll { it.title =~ /Groovy/ }.'@id'*.toInteger()
assert ['1-932394-84-2', '0123725070'] == books.book.'meta:isbn'*.toString()