Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 13 Next »

Information

Java has in-built support for DOM processing of XML using classes representing the various parts of XML documents, e.g. Document, Element, NodeList, Attr etc. For more information about these classes, refer to the respective JavaDocs. Some of the key classes are:

DOM class

JavaDocs

Element

1.4 5

NodeList

1.4 5

Groovy syntax benefits can be applied when using these classes. In addition, the DOMCategory class provides the following additional helper methods:

DOM class

Method

Description/Equivalent longhand

Element

.'child'

.getElementsByTagName('child')

Element

.child

.getElementsByTagName('child')

Element

.'*'

.getElementsByTagName('*')

Element

.'@attr'

.getAttribute('attr')

Element

.text()

.firstChild.nodeValue (or textContent if you are using Xerces)

Element

.name()

.nodeName

Element

.parent()

.parentNode

Element

.children()

.childNodes (potentially including whitespace textnodes etc.)

NodeList

.size()

.length

NodeList

[n]

.item(n)

NodeList

.text()

.collect{ it.text() }

NodeList

.child

flattened version of .getElementsByTagName('child') for each node in the NodeList

NodeList

.iterator()

same as for loop, enables closures, e.g. findAll, every, etc.

All these methods return either Element or NodeList classes or String values for attributes. There are no new classes to know about, just some improved syntax.

Example

This example assumes the following class is already on your CLASSPATH:

XmlExamples.groovy

Here is an example of using Groovy's DOMCategory:

  • No labels