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
<h2>Car Example</h2> <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"> <h4>Using Xalan</h4> <p>Here is an example of using Apache <a href="http://xml.apache.org/xalan-j/">Xalan</a> with Groovy to read an existing XML file:</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> // require(groupId:'xalan', artifactId:'xalan', version:'2.6.0') import org.apache.xpath.XPathAPI import javax.xml.parsers.DocumentBuilderFactory messages = [] def processCar(car) { def make = XPathAPI.eval(car, '@make').str() def country = XPathAPI.eval(car, 'country/text()').str() def type = XPathAPI.eval(car, 'record/@type').str() messages << make + ' of ' + country + ' has a ' + type + ' record' } def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder() def inputStream = new ByteArrayInputStream(XmlExamples.CAR_RECORDS.bytes) def records = builder.parse(inputStream).documentElement XPathAPI.selectNodeList(records, '//car').each{ processCar(it) } assert messages == [ 'Holden of Australia has a speed record', 'Peel of Isle of Man has a size record', 'Bugatti of France has a price record' ] </pre></td></tr></table> <h4>Using Jaxen</h4> <p>Here is an example of using <a href="http://jaxen.org/">Jaxen</a> with Groovy to read an existing XML file:</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> // require(groupId:'jaxen', artifactId:'jaxen', version:'1.1-beta-10') import org.jaxen.dom.DOMXPath import javax.xml.parsers.DocumentBuilderFactory messages = [] def processCar(car) { def make = new DOMXPath('@make').stringValueOf(car) def country = new DOMXPath('country/text()').stringValueOf(car) def type = new DOMXPath('record/@type').stringValueOf(car) messages << make + ' of ' + country + ' has a ' + type + ' record' } def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder() def inputStream = new ByteArrayInputStream(XmlExamples.CAR_RECORDS.bytes) def records = builder.parse(inputStream).documentElement new DOMXPath('//car').selectNodes(records).each{ processCar(it) } assert messages == [ 'Holden of Australia has a speed record', 'Peel of Isle of Man has a size record', 'Bugatti of France has a price record' ] </pre></td></tr></table> <p>Note: many libraries (e.g. DOM4J, JDOM, XOM) bundle or provide optional support for Jaxen. You may not need to download any additional JARs to use it.</p> <h4>Using Java's native XPath support</h4> <p>If you are using Java 5 and above, you don't need to use the standalone Xalan jar but instead can use the now built-in XPath facilities: </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> import javax.xml.xpath.* def xpath = XPathFactory.newInstance().newXPath() def nodes = xpath.evaluate( '//car', records, XPathConstants.NODESET ) nodes.each{ def make = xpath.evaluate( '@make', it ) def country = xpath.evaluate( 'country/text()', it ) def type = xpath.evaluate( 'record/@type', it ) messages << "$make of $country has a $type record" } </pre></td></tr></table> <h2>People in Groups Example</h2> <p>Inspired by <a href="http://stage.vambenepe.com/archives/181">this example</a>, here is how to use XPath to determine which groups include all three of <em>alan</em>, <em>paul</em> and <em>ivan</em> for this graph representing group membership.</p> <p><img class="confluence-embedded-image" src="/download/attachments/231082395/graph.png?version=1&modificationDate=1371717529834" data-image-src="/download/attachments/231082395/graph.png?version=1&modificationDate=1371717529834" data-linked-resource-id="231377337" data-linked-resource-type="attachment" data-linked-resource-default-alias="graph.png" data-base-url="http://docs.codehaus.org" data-linked-resource-container-id="231082395" title="null > graph.png"><br /> <span style="color: grey;">Picture source: <a class="external-link" href="http://stage.vambenepe.com/pages/graph.png" rel="nofollow">http://stage.vambenepe.com/pages/graph.png</a></span></p> <p>First, here is the XML data capturing the group membership information:</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 xml = ''' <doc> <person name="alan"><g>1</g><g>2</g><g>4</g></person> <person name="marc"><g>1</g><g>2</g><g>3</g></person> <person name="paul"><g>2</g><g>3</g><g>4</g></person> <person name="ivan"><g>2</g><g>4</g></person> <person name="eric"><g>4</g></person> </doc> ''' </pre></td></tr></table> <h4>Using Java's native XPath support</h4> <p>Here is how to check that groups 2 and 4 are the groups in question using the built-in XPath capabilities of Java 5 and above:</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> import javax.xml.parsers.DocumentBuilderFactory import javax.xml.xpath.* xpath = ''' /doc/person[@name="alan"]/g [.=/doc/person[@name="paul"]/g] [.=/doc/person[@name="ivan"]/g] ''' builder = DocumentBuilderFactory.newInstance().newDocumentBuilder() doc = builder.parse(new ByteArrayInputStream(xml.bytes)) expr = XPathFactory.newInstance().newXPath().compile(xpath) nodes = expr.evaluate(doc, XPathConstants.NODESET) assert nodes.collect { node -> node.textContent } == ['2', '4'] </pre></td></tr></table> <p>And you can refactor your XPath Expression with something like:</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> import javax.xml.parsers.DocumentBuilderFactory import javax.xml.xpath.* groupsOf = { name -> "/doc/person[@name='$name']/g" } xpath = "${groupsOf 'alan'}[.=${groupsOf 'paul'}][.=${groupsOf 'ivan'}]" builder = DocumentBuilderFactory.newInstance().newDocumentBuilder() doc = builder.parse(new ByteArrayInputStream(xml.bytes)) expr = XPathFactory.newInstance().newXPath().compile(xpath) nodes = expr.evaluate(doc, XPathConstants.NODESET) assert nodes.collect { node -> node.textContent } == ['2', '4'] </pre></td></tr></table> <h4>Using GPath</h4> <p>Of course, you can also do this without XPath by using Groovy's GPath facilities as follows:</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 root = new XmlParser().parseText(xml) Set groups = root.person.g.collect{ it.text() } assert groups.findAll{ group -> root.person.g.findAll{ it.text() == group }.'..'.'@name'.containsAll(['alan', 'paul', 'ivan']) } == ['2', '4'] </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