Chapter 13. Working with XML

 

This chapter covers:

  • 13.1 Background: XML-processing libraries
  • 13.2 Writing XML
  • 13.3 Validating XML
  • 13.4 Reading XML data
  • 13.5 Transforming XML with XSLT
  • 13.6 Summary

In this chapter we’re going to take a partial break from the web site and do some low-level XML work. Many Java projects need some XML processing at build time. It may be the need to create configuration files on the fly, or it could be the desire to generate Java source files from XML. This is essentially what all web service stacks do when they create Java classes from the XML files that describe the remote SOAP service.

We’re going to explore XML processing in Ant. We’ll see how to use Ant to validate the Atom feed of events coming off our diary web application, and to generate Java source files from XML templates. We’ll also look at the basic ways that Ant can work with XML.

Ant can perform four main actions with XML data:

  • Write some XML into a file
  • Validate an XML document
  • Read Ant properties from an XML file
  • Transform XML content into other XML documents, HTML files, or plain text

This will give a taste of Ant’s abilities and show its limits too. It will demonstrate where a bit of XML can benefit a Java application at build time, and how Ant can help.

13.1. Background: XML-processing libraries

Before beginning on XML processing, here’s a quick recap of the current state of XML parsing in Java. This can explain why XML parsing and processing don’t always work.

13.2. Writing XML

13.3. Validating XML

13.4. Reading XML data

13.5. Transforming XML with XSLT

13.6. Summary