This chapter covers
- Reading and writing XML and JSON
- Transforming treelike structures in-place and as streams
- Navigating inside structured data
Perfection is achieved not when you have nothing more to add, but when you have nothing left to take away.
Antoine de Saint-Exupéry
Computing means applying logic to data. We retrieve data, process it, and store it again. Processing is the essential activity while retrieving and storing are just necessities. All the aspects of various data formats, encodings, and access protocols can get complex, though, and the required work is likely to distract us. Groovy helps us to stay focused by making the data handling part almost transparent.
We’re going to explore two data serialization formats that have special built-in support in the Groovy standard library: XML and JSON. Most of the chapter is devoted to XML, which is the more mature approach and has more options available for its production and consumption. JSON is another popular serialization format for semistructured data. We’ll briefly cover the details of processing JSON very efficiently at the end of the chapter.
XML, the eXtensible Markup Language, is so commonly used these days that it’s hard to believe there were times without it. The World Wide Web Consortium (W3C) standardized the first version of XML in 1996.