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.
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.