Groovy web console

subscribe to the feed Subscribe
to this

XmlSlurper with Namespaces

Published 1 decade ago by mrhaki with tags xml
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.xml.*

def xml = '''
<books xmlns:meta="http://meta/book/info" count="3">
  <book id="1">
    <title lang="en">Groovy in Action</title>
  <book id="2">
    <title lang="en">Groovy Programming</title>
  <book id="3">
    <title>Groovy & Grails</title>
    <!--Not yet available.-->
  <book id="4">
    <title>Griffon Guide</title>

def books = new XmlSlurper().parseText(xml).declareNamespace([meta:'http://meta/book/info'])
assert books instanceof groovy.util.slurpersupport.GPathResult
assert 4 ==
assert 11 == books.breadthFirst().size()
assert 'Groovy in Action' ==[0].title.toString()
assert 'Groovy Programming' == { it.@id == '2' }.title as String
assert [1, 2, 3] == { it.title =~ /Groovy/ }.'@id'*.toInteger()
assert ['1-932394-84-2', '0123725070'] =='meta:isbn'*.toString()