Chapter 9. Document Creation by Programming in Java

Table of Contents

9.1. Creating a DOM Document
9.2. Creating a Document with SAX Events
9.3. Creating a Document with StAX streaming
9.4. Additional Information on XML Document Creation

In the previous section, we have shown how to parse an existing XML document by programming. It is also important to see how an XML document can be created by programming. We will therefore show the inverse of the programs shown previously by writing a program that parses the compact form we produced and expand it into the corresponding XML document. In principle, after compaction and expansion, we should recover the original XML document with which we started but since we have not faithfully transformed whitespace, the files are not strictly identical. Although one can write a program to create a file with XML tags and their content using print methods, we will show (Section 9.1) that it is both simpler and more systematic to create an XML document in memory using the DOM model, to modify it and then to print it using a serializer. In Section 9.2, we will describe how to create an XML document by parsing a text file and send SAX events to a transformer and in Section 9.3 we will show how to do the same with the StAX streaming approach.