--- title: Groovy - Reading XML using Groovy's XmlSlurper tags: - IT/Development/Groovy --- This example assumes the following class is already on your CLASSPATH: XmlExamples.groovy ```groovy class XmlExamples { static def CAR_RECORDS = ''' Australia Production Pickup Truck with speed of 271kph Isle of Man Smallest Street-Legal Car at 99cm wide and 59 kg in weight France Most Valuable Car at $15 million ''' } ``` Here is an example of using XmlSlurper: ```groovy def records = new XmlSlurper().parseText(XmlExamples.CAR_RECORDS) def allRecords = records.car assert 3 == allRecords.size() def allNodes = records.depthFirst().collect{ it } assert 10 == allNodes.size() def firstRecord = records.car[0] assert 'car' == firstRecord.name() assert 'Holden' == firstRecord.@make.text() assert 'Australia' == firstRecord.country.text() def carsWith_e_InMake = records.car.findAll{ it.@make.text().contains('e') } assert carsWith_e_InMake.size() == 2// alternative way to find cars with 'e' in makeassert 2 == records.car.findAll{ it.@make =~ '.*e.*' }.size() // makes of cars that have an 's' followed by an 'a' in the countryassert ['Holden', 'Peel'] == records.car.findAll{ it.country =~ '.*s.*a.*' }.@make.collect{ it.text() } def expectedRecordTypes = ['speed', 'size', 'price'] assert expectedRecordTypes == records.depthFirst().grep{ it.@type != '' }.'@type'*.text() assert expectedRecordTypes == records.'**'.grep{ it.@type != '' }.'@type'*.text() def countryOne = records.car[1].country assert 'Peel' == countryOne.parent().@make.text() assert 'Peel' == countryOne.'..'.@make.text() // names of cars with records sorted by yeardef sortedNames = records.car.list().sort{ it.@year.toInteger() }.'@name'*.text() assert ['Royale', 'P50', 'HSV Maloo'] == sortedNames assert ['Australia', 'Isle of Man'] == records.'**'.grep{ it.@type =~ 's.*' }*.parent().country*.text() assert 'co-re-co-re-co-re' == records.car.children().collect{ it.name()[0..1] }.join('-') assert 'co-re-co-re-co-re' == records.car.'*'.collect{ it.name()[0..1] }.join('-') ``` Notes: - If your elements contain characters such as dashes, you can enclose the element name in double quotes. Meaning for: ```xml test ``` You do the following: ```groovy def foo = new XmlSlurper().parseText(FOO_XML) assert "test" == foo."foo-bar".text() ``` You can also parse XML documents using namespaces: ```groovy def wsdl = ''' ''' def xml = new XmlSlurper().parseText(wsdl).declareNamespace(ns1: 'http://www.example.org/NS1', ns2: 'http://www.example.org/NS2') println xml.'ns1:message'.'ns1:part'.size() println xml.'ns2:message'.'ns2:part'.size() ``` XmlSlurper has a declareNamespace method which takes a Map of prefix to URI mappings. You declare the namespaces and just use the prefixes in the GPath expression. ```groovy new XmlSlurper().parseText(blog).declareNamespace(dc: "http://purl.org/dc/elements/1.1/").channel.item.findAll { item -> d.any{entry -> item."dc:date".text() =~ entry.key} && a.any{entry -> item.tags.text() =~ entry } ``` Some remarks: - name or "*:name" matches an element named "name" irrespective of the namespace it's in (i.e. this is the default mode of operation) - ":name" matches an element named "name" only id the element is not in a namespace - "prefix:name" matches an element names "name" only if it is in the namespace identified by the prefix "prefix" (and the prefix to namespace mapping was defined by a previous call to declareNamespace) You can generate namespaced elements in StreamingMarkupBuilder very easily: ```groovy System.out << new StreamingMarkupBuilder().bind { mkp.declareNamespace(dc: "http://purl.org/dc/elements/1.1/") root { dc.date() } } ```