Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>This example assumes the following class is already on your CLASSPATH:</p> <img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2luY2x1ZGU6WE1MIEV4YW1wbGV9&locale=en_GB&version=2" data-macro-name="include" data-macro-default-parameter="XML Example"> <p>Here is an example of using XmlSlurper:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> 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 make assert 2 == records.car.findAll{ it.@make =~ '.*e.*' }.size() // makes of cars that have an 's' followed by an 'a' in the country assert ['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 year def 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('-') </pre></td></tr></table> <p>Notes:</p> <ul> <li>If your elements contain characters such as dashes, you can enclose the element name in double quotes. Meaning for:</li> </ul> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <foo> <foo-bar>test</foo-bar> </foo> </pre></td></tr></table> <ul> <li>You do the following:<br class="atl-forced-newline" /> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def foo = new XmlSlurper().parseText(FOO_XML) assert "test" == foo."foo-bar".text() </pre></td></tr></table> <br class="atl-forced-newline" /> <br class="atl-forced-newline" /></li> </ul> <p>You can also parse XML documents using namespaces:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def wsdl = ''' <definitions name="AgencyManagementService" xmlns:ns1="http://www.example.org/NS1" xmlns:ns2="http://www.example.org/NS2"> <ns1:message name="SomeRequest"> <ns1:part name="parameters" element="SomeReq" /> </ns1:message> <ns2:message name="SomeRequest"> <ns2:part name="parameters" element="SomeReq" /> </ns2:message> </definitions> ''' 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() </pre></td></tr></table> <p>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.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> 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 } </pre></td></tr></table> <p>Some remarks:</p> <ul> <li>name or "*:name" matches an element named "name" irrespective of the namespace it's in (i.e. this is the default mode of operation)</li> <li>":name" matches an element named "name" only id the element is not in a namespace</li> <li>"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)</li> </ul> <p>You can generate namespaced elements in StreamingMarkupBuilder very easily:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> System.out << new StreamingMarkupBuilder().bind { mkp.declareNamespace(dc: "http://purl.org/dc/elements/1.1/") root { dc.date() } } </pre></td></tr></table>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced