Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Andrey Adamovich - Groovy 2 Cookbook - 2013.pdf
Скачиваний:
44
Добавлен:
19.03.2016
Размер:
26.28 Mб
Скачать

5

Working with XML in Groovy

In this chapter, we will cover the following recipes:

ff

ff

ff

ff

ff

ff

ff

ff

ff

Reading XML using XmlSlurper Reading XML using XmlParser

Reading XML content with namespaces Searching in XML with GPath Searching in XML with XPath Constructing XML content

Modifying XML content Sorting XML nodes

Serializing Groovy Beans to XML

Introduction

Dealing with XML in Java is notoriously a tedious business. The Java architects traded flexibility for simplicity, and the Java XML APIs are considered among the lower peaks of the language. Fortunately, Groovy offers a marvelous alternative for reading and producing XML. It's so good that once you experience Groovy's native parsers and emitters, you'll wonder why you used anything else.

www.it-ebooks.info

Working with XML in Groovy

This chapter is divided into recipes that deal with reading XML and producing XML. The two Groovy parsers, XmlParser and XmlSlurper, are discussed in detail as well as the two

XML producers, MarkupBuilder and StreamingMarkupBuilder. We also touch on advanced topics such as the serialization of Java and Groovy objects to XML, and element tree navigation with XPath and GPath.

Reading XML using XmlSlurper

The eXtensible Markup Language or simply XML is the standard data format for exchanging information among computer systems. The first two recipes of this chapter show how to parse XML using Groovy. There are two parsers available in the groovy.util package, XmlParser and XmlSlurper. They both expose similar API; but there are use cases for when it is more appropriate to use one or the other. In this recipe, we look at how to read XML with XmlSlurper and its main peculiarities.

Getting ready

For the examples in the rest of this recipe, we will work with an XML document (shown in the following code) containing a list of works from William Shakespeare. The document is named

shakespeare.xml:

<?xml version="1.0" ?>

<bib:bibliography xmlns:bib="http://bibliography.org" xmlns:lit="http://literature.org">

<bib:author>William Shakespeare</bib:author> <lit:play>

<lit:year>1589</lit:year>

<lit:title>The Two Gentlemen of Verona.</lit:title> </lit:play>

<lit:play>

<lit:year>1594</lit:year>

<lit:title>Love's Labour's Lost.</lit:title> </lit:play>

<lit:play>

<lit:year>1594</lit:year> <lit:title>Romeo and Juliet.</lit:title>

</lit:play>

<lit:play>

<lit:year>1595</lit:year>

<lit:title>A Midsummer-Night's Dream.</lit:title> </lit:play>

</bib:bibliography>

168

www.it-ebooks.info

Chapter 5

How to do it...

Let's go through the process of parsing the previously mentioned XML file:

1.One way to read XML data using XmlSlurper is to create an instance of the class and pass a java.io.File object, which references the file we want to read, into the parse method:

def xmlSource = new File('shakespeare.xml')

def bibliography = new XmlSlurper().parse(xmlSource)

2.The parse method returns an implementation of groovy.util. slurpersupport.GPathResult, which can be used to navigate the XML element tree. For example, the following code will print the text representation of the author element:

println bibliography.author

3.Deeper elements and element collections can also be referenced with the help of the "." operator. Also, a set of finder and iterator methods are available to build complex search expressions:

bibliography.play

.findAll { it.year.toInteger() > 1592 }

.each { println it.title }

The expressions that are used to navigate (and eventually also modify) the XML tree are referred to as GPath expressions. More examples of those expressions can be found in the Searching in XML with GPath recipe.

4.The output of the script should be as follows:

William Shakespeare Love's Labour's Lost. Romeo and Juliet.

A Midsummer-Night's Dream.

How it works...

The previous example selects all the plays written after 1592 and prints their titles.

Groovy's XmlSlurper resides in the groovy.util package, which is imported automatically by Groovy. That's why we do not need an import statement for that class.

169

www.it-ebooks.info

Working with XML in Groovy

XmlSlurper is a SAX-based parser; it loads the full document in memory, but it doesn't require extra memory to process the document using GPath. GPath expressions are lazily evaluated and no extra objects are created when evaluating the expression. XmlSlurper is also null-safe: when accessing an attribute that doesn't exist, it returns an empty string; the same goes for a non-existing node.

As a rule of thumb, you want to use XmlSlurper when you intend to process only a small part of the document; while it is more efficient to use XmlParser when you have to process the whole XML.

See also

ff Reading XML using XmlParser ff Searching in XML with GPath

ff http://groovy.codehaus.org/api/groovy/util/XmlSlurper.html

ff http://groovy.codehaus.org/api/groovy/util/slurpersupport/ GPathResult.html

ff http://groovy.codehaus.org/Processing+XML

Reading XML using XmlParser

In the previous recipe, Reading XML using XmlSlurper, we learned how to read an XML document using the XmlSlurper provided by Groovy. Now it's time to look at the other parser available in Groovy, groovy.util.XmlParser. Its internal implementation differs from groovy.util.XmlSlurper, but it exposes a very similar API when it comes to document parsing, navigation, and modification.

In this recipe, we will cover the essential usage scenarios for the XmlParser class and its differences from XmlSlurper.

How to do it...

Let's use the same shakespeare.xml file we used in the Reading XML using XmlSlurper recipe.

1.Reading XML data is very similar to XmlSlurper. You need to create an instance of

XmlParser and pass a file reference to its parse method as shown:

def xmlSource = new File('shakespeare.xml')

def bibliography = new XmlParser().parse(xmlSource)

170

www.it-ebooks.info

Chapter 5

2.As with XmlSlurper, GPath expressions (see the Searching in XML with GPath recipe for more advanced examples) are also possible with XmlParser. For example, the code to print the titles of all plays written after 1592 would be as follows:

println bibliography.'bib:author'.text()

bibliography.'lit:play'

.findAll { it.'lit:year'

.text().toInteger() > 1592 }

.each { println it.'lit:title'.text() }

3.The output of the script will be the same as in the previous recipe:

William Shakespeare Love's Labour's Lost. Romeo and Juliet.

A Midsummer-Night's Dream.

How it works...

Navigating XML data with XmlParser is slightly different from XmlSlurper. In order to find an element, you need to use its fully qualified name (FQN) including the exact prefix. Since, in our XML example, we use the bib: prefix for the author element, we need to refer to author's data as bib:author (or as *:author to be more independent).

If our XML example didn't contain a FQN, then we could have referred to them in a very similar way as we did for XmlSlurper, for example, bibliography.author.

In step 2, you may have noticed that we have used the text method to get the textual representation of the author element. That's because XmlParser returns instances of groovy.util.Node, whose toString method does not return the element's textual content by default. There is also the attribute method that accepts a name and returns the given attribute. If you ask for an attribute that doesn't exist, attribute returns null (this is the opposite behavior of XmlSlurper that returns an empty string).

The main difference between XmlParser and XmlSlurper is that the first uses the groovy.util.Node type and its GPath expressions result in lists of nodes, which are easily manipulable using our knowledge of lists and collections. Compared to XmlSlurper,

XmlParser consumes more memory because it has to create an intermediate data structure to represent the node tree, but it makes XML tree queries a bit faster. So, it's up to developers to decide which implementation better suits their needs.

171

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]