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>Information</h2> <p>Java has in-built support for DOM processing of XML using classes representing the various parts of XML documents, e.g. <em>Document</em>, <em>Element</em>, <em>NodeList</em>, <em>Attr</em> etc. For more information about these classes, refer to the respective JavaDocs. Some of the key classes are:</p> <table class="confluenceTable"><tbody> <tr> <th class="confluenceTh"><p>DOM class</p></th> <th class="confluenceTh"><p>JavaDocs</p></th> </tr> <tr> <td class="confluenceTd"><p>Element</p></td> <td class="confluenceTd"><p><a href="http://download.oracle.com/javase/6/docs/api/org/w3c/dom/Element.html">6</a> <a href="http://download.oracle.com/javase/7/docs/api/org/w3c/dom/Element.html">7</a></p></td> </tr> <tr> <td class="confluenceTd"><p>NodeList</p></td> <td class="confluenceTd"><p><a href="http://download.oracle.com/javase/6/docs/api/org/w3c/dom/NodeList.html">6</a> <a href="http://download.oracle.com/javase/7/docs/api/org/w3c/dom/NodeList.html">7</a></p></td> </tr> </tbody></table> <p>Groovy syntax benefits can be applied when using these classes resulting in code which is similar to but more compact than the Java equivalent. In addition, Groovy supports the following built-in helper method for these classes.</p> <table class="confluenceTable"><tbody> <tr> <th class="confluenceTh"><p>DOM class</p></th> <th class="confluenceTh"><p>Method</p></th> <th class="confluenceTh"><p>Description/Equivalent longhand</p></th> </tr> <tr> <td class="confluenceTd"><p><em>NodeList</em></p></td> <td class="confluenceTd"><p><em>.iterator()</em></p></td> <td class="confluenceTd"><p>same as for loop, enables closures, e.g. <em>findAll</em>, <em>every</em>, etc.</p></td> </tr> </tbody></table> <p>In addition, the <em>DOMCategory</em> class provides numerous additional helper methods and syntax shortcuts:</p> <table class="confluenceTable"><tbody> <tr> <th class="confluenceTh"><p>DOM class</p></th> <th class="confluenceTh"><p>Method</p></th> <th class="confluenceTh"><p>Description/Equivalent longhand</p></th> </tr> <tr> <td class="confluenceTd"><p><em>Element</em></p></td> <td class="confluenceTd"><p><em>.'child'</em> or <em>.child</em> or <em>['child']</em></p></td> <td class="confluenceTd"><p>similar to <em>.getElementsByTagName('child')</em> but only gets direct children</p></td> </tr> <tr> <td class="confluenceTd"><p><em>Element</em></p></td> <td class="confluenceTd"><p><em>.children()</em> or <em>.'*'</em> or <em>['*']</em></p></td> <td class="confluenceTd"><p>special case of above which finds all children regardless of tagname (plus text nodes)</p></td> </tr> <tr> <td class="confluenceTd"><p><em>Element</em></p></td> <td class="confluenceTd"><p><em>.'@attr'</em> or <em>['@attr']</em></p></td> <td class="confluenceTd"><p><em>.getAttribute('attr')</em></p></td> </tr> <tr> <td class="confluenceTd"><p><em>Element</em></p></td> <td class="confluenceTd"><p><em>.attributes()</em></p></td> <td class="confluenceTd"><p>equivalent to <em>.attributes</em> returns a <em>NamedNodeMap</em></p></td> </tr> <tr> <td class="confluenceTd"><p><em>Element</em></p></td> <td class="confluenceTd"><p><em>.text()</em></p></td> <td class="confluenceTd"><p><em>.firstChild.nodeValue</em> (or <em>textContent</em> if you are using Xerces)</p></td> </tr> <tr> <td class="confluenceTd"><p><em>Element</em></p></td> <td class="confluenceTd"><p><em>.name()</em></p></td> <td class="confluenceTd"><p><em>.nodeName</em></p></td> </tr> <tr> <td class="confluenceTd"><p><em>Element</em></p></td> <td class="confluenceTd"><p><em>.parent()</em> or <em>.'..'</em> or <em>['..']</em></p></td> <td class="confluenceTd"><p><em>.parentNode</em></p></td> </tr> <tr> <td class="confluenceTd"><p><em>Element</em></p></td> <td class="confluenceTd"><p><em>.depthFirst()</em> or <em>.'**'</em></p></td> <td class="confluenceTd"><p>depth-first traversal of nested children</p></td> </tr> <tr> <td class="confluenceTd"><p><em>Element</em></p></td> <td class="confluenceTd"><p><em>.breadthFirst()</em></p></td> <td class="confluenceTd"><p>breadth-first traversal of nested children</p></td> </tr> <tr> <td class="confluenceTd"><p><em>Node</em></p></td> <td class="confluenceTd"><p><em>.toString()</em></p></td> <td class="confluenceTd"><p>text node value as a <em>String</em></p></td> </tr> <tr> <td class="confluenceTd"><p><em>NodeList</em></p></td> <td class="confluenceTd"><p><em>.size()</em></p></td> <td class="confluenceTd"><p><em>.length</em></p></td> </tr> <tr> <td class="confluenceTd"><p><em>NodeList</em></p></td> <td class="confluenceTd"><p><em>.list()</em></p></td> <td class="confluenceTd"><p>converted to a list of nodes</p></td> </tr> <tr> <td class="confluenceTd"><p><em>NodeList</em></p></td> <td class="confluenceTd"><p><em>[n]</em></p></td> <td class="confluenceTd"><p><em>.item(n)</em></p></td> </tr> <tr> <td class="confluenceTd"><p><em>NodeList</em></p></td> <td class="confluenceTd"><p><em>.text()</em></p></td> <td class="confluenceTd"><p><em>.collect{ it.text() }</em></p></td> </tr> <tr> <td class="confluenceTd"><p><em>NodeList</em></p></td> <td class="confluenceTd"><p><em>.child</em></p></td> <td class="confluenceTd"><p>flattened version of <em>.child</em> for each node in the <em>NodeList</em></p></td> </tr> <tr> <td class="confluenceTd"><p><em>NamedNodeMap</em></p></td> <td class="confluenceTd"><p><em>.size()</em></p></td> <td class="confluenceTd"><p><em>.length</em></p></td> </tr> <tr> <td class="confluenceTd"><p><em>NamedNodeMap</em></p></td> <td class="confluenceTd"><p><em>.'child'</em> or <em>.child</em> or <em>['child']</em></p></td> <td class="confluenceTd"><p><em>.getNamedItem(elementName).nodeValue</em></p></td> </tr> </tbody></table> <p>All these methods return standard Java classes (e.g. <em>String</em> and <em>List</em>) or standard DOM classes (e.g. <em>Element</em>, <em>NodeList</em>), so there are no new classes to learn, just some improved syntax.</p> <h2>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"> <p>Here is an example of using Groovy's DOMCategory:</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 groovy.xml.DOMBuilder import groovy.xml.dom.DOMCategory messages = [] def processCar(car) { assert car.name() == 'car' def make = car.'@make' def country = car.country[0].text() def type = car.record[0].'@type' messages << make + ' of ' + country + ' has a ' + type + ' record' } def reader = new StringReader(XmlExamples.CAR_RECORDS) def doc = DOMBuilder.parse(reader) def records = doc.documentElement use (DOMCategory) { assert 3 == records.'*'.size() def cars = records.'car' assert cars[0].parent() == records assert 3 == cars.size() assert 2 == cars.findAll{ it.'@year'.toInteger() > 1950 }.size() def carsByCentury = cars.list().groupBy{ it.'@year'.toInteger() >= 2000 ? 'this century' : 'last century' } assert 1 == carsByCentury['this century'].size() assert 2 == carsByCentury['last century'].size() cars.each{ car -> processCar(car) } } 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>
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