The first application you will create, RssWriter, creates an XML document that contains the start of an RSS feed. The document is shown in Listing 19.2.
The base nu.xom package contains classes for a complete XML document (Document) and the nodes a document can contain (Attribute, Comment, DocType, Element, ProcessingInstruction, and Text).The RssStarter application uses several of these classes. First, an Element object is created by specifying the element’s name as an argument:
Element rss = new Element(“rss”);
This statement creates an object for the root element of the document, rss. Element’s one-argument constructor can be used because the document does not employ a feature of XML called namespaces; if it did, a second argument would be necessary: the namespace uniform resource identifier (URI) of the element. The other classes in the XOM library support namespaces in a similar manner.
In the XML document in Listing 19.2, the rss element includes an attribute named version with the value “2.0”. An attribute can be created by specifying its name and value in consecutive arguments:
Attribute version = new Attribute(“version”, “2.0”);
Attributes are added to an element by calling its addAttribute() method with the attribute as the only argument:
rss.addAttribute(version);
The text contained within an element is represented by the Text class, which is constructed by specifying the text as a String argument:
Text titleText = new Text(“Workbench”);
When composing an XML document, all of its elements end up inside a root element that is used to create a Document object—a Document constructor is called with the root element as an argument. In the RssStarter application, this element is called rss. Any Element object can be the root of a document:
Document doc = new Document(rss);
In XOM’s tree structure, the classes representing an XML document and its constituent parts are organized into a hierarchy below the generic superclass nu.xom.Node. This class has three subclasses in the same package:
Attribute, LeafNode, and ParentNode. To add a child to a parent node, call the parent’s appendChild() method with the node to add as the only argument. The following code creates three elements—a parent called domain and two of its children, name and dns:
Element channel = new Element(“channel”);
Element link = new Element(“link”);
Text linkText = new Text(“http://www.cadenhead.org/workbench/”);
link.appendChild(linkText);
channel.appendChild(link);
The appendChild() method appends a new child below all other children of that parent. The preceding statements produce this XML fragment:
<channel>
<link>http://www.cadenhead.org/workbench/</link>
</channel>
The appendChild() method also can be called with a String argument instead of a node. A Text object representing the string is created and added to the element:
link.appendChild(“http://www.cadenhead.org/workbench/”);
After a tree has been created and filled with nodes, it can be displayed by calling the Document method toXML(), which returns the complete and well-formed XML document as a String.
Listing 19.3 shows the complete application.
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 spacesbetween 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.