Java 7 for Absolute Beginners

(nextflipdebug5) #1
CHAPTER 9 ■ WRITING AND READING XML

As you saw in Chapter 8, Java uses Stream objects for reading from, and writing to, files. Since you’re
already processing Stream objects, saving an XML document as a file is a natural and easy task. Reading
an existing file as a stream also makes loading an XML file straightforward. So, there are streams in,
streams out, and streams between the steps of larger processes. Fortunately, Java makes working with
streams easy.


DOM and SAX


DOM (Document Object Model) and SAX (Simplified API for XML) have their strengths and weaknesses.
As with most data-related problems, knowing which one to use comes down to knowing your data. If
you need to work with relatively small documents, DOM works well, as it loads the entire XML data
stream into memory, making it fast (again, provided the document is small). SAX, on the other hand,
uses only enough memory to process the current element (called a node), which makes it capable of
handling documents of any size. I've used SAX to parse the contents of books as long as 2,000 pages. The
down side of SAX is that you can't reach much of the document at once, as little of it is in memory. Also,
SAX only works with incoming XML documents; it doesn't write XML.
So, remember to use DOM for small XML sources and SAX for larger XML sources. If you're
uncertain if the XML input will be large or small, use SAX. Of course, whether a document is large or
small depends on how much memory is available for processing the document. If your program can run
on a computer with plenty of memory all to itself, you can use DOM to load fairly large documents.
However, if your program has to share a server with other processes, or has to run on a small device
(such as a phone), memory will be limited and your options will be reduced. Finally, if your application
has to process multiple documents at once (perhaps for multiple users), the memory for each process
will be greatly reduced. The more constrained the memory available to the application, the more you
should lean toward SAX. As an example, I recently worked on an application that would trigger an
arbitrary number of transforms to create sets of documents. In practice, each set contained about 15
documents. Also, multiple users could start document-production runs at the same time, leading to as
many as 50 documents being processed at the same time, all with 8 MB of RAM. We definitely needed to
use SAX.


Writing XML


As mentioned above, you can write XML with the DOM package or by writing String objects. If you just
need to write an XML file, writing strings works well enough (and it is the fastest way to create XML). On
the other hand, if you need to pass your XML output to a process that requires an XML header, and
perhaps even needs to ensure that the XML conforms to a schema, you might want to consider using
DOM.
Before you get to writing XML, you first need a data source to provide the content that you want to
turn into XML. Here is a simple class that provides the content of the poem used earlier (“The Great
Figure,” by William Carlos Williams).


Listing 9-3. A Poem As a Data Source


package com.bryantcs.examples.xml;


import java.util.ArrayList;

Free download pdf