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
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>GPath is a path expression language integrated into Groovy which allows parts of nested structured data to be identified. In this sense, it has similar aims and scope as XPath does for XML. The two main places where you use GPath expressions is when dealing with nested POJOs or when dealing with XML.</p> <p>As an example, you can specify a path to an object or element of interest:<br /> a.b.c -> for XML, yields all the <c> elements inside <b> inside <a><br /> a.b.c -> all POJOs, yields the <c> properties for all the <b> properties of <a> (sort of like a.getB().getC() in JavaBeans)</p> <p>For XML, you can also specify attributes, e.g.:<br /> a["@href"] -> the href attribute of all the a elements<br /> a.'@href' -> an alternative way of expressing this<br /> a.@href -> an alternative way of expressing this when using <code>XmlSlurper</code></p> <h3>Example</h3> <p>The best example of GPath for xml is test-new/groovy/util/XmlSlurperTest.groovy.</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> package groovy.util class XmlSlurperTest extends GroovyTestCase { void testXmlParser() { def text = """ <characters> <props> <prop>dd</prop> </props> <character id="1" name="Wallace"> <likes>cheese</likes> </character> <character id="2" name="Gromit"> <likes>sleep</likes> </character> </characters> """ def node = new XmlSlurper().parseText(text); assert node != null assert node.children().size() == 3 //, "Children ${node.children()}" def characters = node.character println "node:" + node.children().size() println "characters:" + node.character.size() for (c in characters) { println c['@name'] } assert characters.size() == 2 assert node.character.likes.size() == 2 //, "Likes ${node.character.likes}" // lets find Gromit def gromit = node.character.find { it['@id'] == '2' } assert gromit != null //, "Should have found Gromit!" assert gromit['@name'] == "Gromit" // lets find what Wallace likes in 1 query def answer = node.character.find { it['@id'] == '1' }.likes.text() assert answer == "cheese" } } </pre></td></tr></table> <h3>Outline</h3> <h5>1.Accessing element as property</h5> <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 characters = node.character def gromit = node.character[1] </pre></td></tr></table> <h5>2.Accessing attributes</h5> <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> println gromit['@name'] or println gromit.@name </pre></td></tr></table> <h5>3.Accessing element body</h5> <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> println gromit.likes[0].text() println node.text() </pre></td></tr></table> <p>If the element is a father node,it will print all children's text.</p> <h5>3.Explore the DOM use children() and parent()</h5> <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 characters = node.children() for (c in characters) { println c.@name } </pre></td></tr></table> <h5>4.Find elements use expression</h5> <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 gromit = node.character.find { it.@id == '2' } </pre></td></tr></table> <h3>Another Example</h3> <p>Here is a two line example of how to get a list of all the links to .xml files listed on a web page. The Neko parser is used to parse non-well formed html. It no longer ships as part of the standard Groovy distribution but can be <a href="http://sourceforge.net/projects/nekohtml">downloaded</a> and dropped into the lib directory of your Groovy distribution. You'll need to also add a copy of xercesImpl.jar to the groovy lib directory.</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 myDocument = new XmlParser( new org.cyberneko.html.parsers.SAXParser() ).parse("http://myUrl.com") def links = myDocument.depthFirst().A['@href'].findAll{ it.endsWith(".xml") } </pre></td></tr></table> <h3>More Information</h3> <p>See also: <a class="confluence-link" href="/display/GROOVY/Processing+XML" data-linked-resource-id="63273" data-linked-resource-type="page" data-linked-resource-default-alias="Processing XML" data-base-url="http://docs.codehaus.org">Processing XML</a></p>
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