Java Reference
In-Depth Information
LISTING 19.3
Continued
29: System.out.println(doc.toXML());
30: }
31: }
The RssStarter application displays the XML document it creates on standard output.
The following command runs the application and redirects its output to a file called
feed.rss :
java RssStarter > feed.rss
XOM automatically precedes a document with an XML declaration.
The XML produced by this application contains no indentation; elements are stacked on
the same line.
XOM only preserves significant whitespace when representing XML data—the spaces
between elements in the RSS feed contained in Listing 19.2 are strictly for presentation
purposes and are not produced automatically when XOM creates an XML document. A
subsequent example demonstrates how to control indentation.
Modifying an XML Document
The next project, the DomainEditor application, makes several changes to the XML doc-
ument that was just produced by the RssStarter application, feed.rss . The text
enclosed by the link element is changed to “http://www.cadenhead.org/”, and a new
item element is added:
19
<item>
<title>Fuzzy Zoeller Sues Over Libelous Wikipedia Page</title>
</item>
Using the nu.xom package, XML documents can be loaded into a tree from several
sources: a File , InputStream , Reader , or a URL (which is specified as a String instead
of a java.net.URL object).
The Builder class represents a SAX parser that can load an XML document into a
Document object. Constructor methods can be used to specify a particular parser or let
XOM use the first available parser from this list: Xerces 2, Crimson, Piccolo, GNU
Aelfred, Oracle, XP, Saxon Aelfred, or Dom4J Aelfred. If none of these is found, the
parser specified by the system property org.xml.sax.driver is used. Constructors also
determine whether the parser is validating or nonvalidating.
 
Search WWH ::




Custom Search