Home arrow Java arrow Using XML in a Java Context

Using XML in a Java Context

For those who want to explore Java and XML, this article helps you with creating an XML document and other XML functions in the context of Java. The third of three parts, it is excerpted from chapter 20 of the book Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280).

Author Info:
By: Sams Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
June 22, 2006
  1. · Using XML in a Java Context
  2. · Modifying an XML Document
  3. · Formatting an XML Document
  4. · Evaluating XOM
  5. · Summary

print this article

Using XML in a Java Context
(Page 1 of 5 )

Creating an XML Document

The first application you will create, Domains, creates an XML document that contains several kinds of configuration information for a domain name. The document is shown in Listing 20.6.

Listing 20.6 The Full Text of domains.xml

 1: <?xml version="1.0"?>
2: <domains>
3:  <domain>
4:   <name>java21days.com</name>
5:   <dns>
6:    <ttl>86400</ttl>
7:    <ip></ip>
8:   </dns>
9:   <webdir status="parked">/web/
java21days</webdir> 10: </domain> 11: </domains>

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 Domains application uses several of these classes. First, Element objects are created by specifying the element's name as an argument:

Element domains = new Element("domains");

This statement creates an object for the root element of the document, domains. 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 URI of the element. The other classes in the XOM library support namespaces in a similar manner.

In the XML document in Listing 20.6, the webdir element includes an attribute named status with the value "parked". An attribute can be created by specifying its name and value in consecutive arguments:

Attribute status = new Attribute("status",

The text contained within an element is represented by the Text class, which is constructed by specifying the text as a String argument:

Text ttlText = new Text("86400");

All these elements end up inside a root element, which is used to create a Document object—a Document constructor is called with the root element as an argument. In the Domains application, this element is called domains. Any Element object can be the root of a document:

Document doc = new Document(domains);

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 domain = new Element("domain");
Element name = new Element("name");
Element dns = new Element("dns");

The appendChild() method appends a new child below all other children of that parent. The preceding statements produce this XML fragment:

<name />
<dns />

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:


Attributes are distinct from the other nodes and require a different method. To add one to a node, call the node's addAttribute() method with the attribute as an argument:

Attribute status = new Attribute("status",
"parked"); webdir.addAttribute(status);

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 20.7 shows the complete application.

Listing 20.7 The Full text of Domains.java

 1: import nu.xom.*;
3: public class Domains {
4:   public static void main(String[] arguments) {
5:     // Create elements, text elements, and an
element attribute 6: Element domains = new Element("domains"); 7: Element domain = new Element("domain"); 8: Element name = new Element("name"); 9: Element dns = new Element("dns"); 10: Element ttl = new Element("ttl"); 11: Element ip = new Element("ip"); 12: Element webdir = new Element("webdir"); 13: Attribute status = new Attribute("status",
"parked"); 14: Text nameText = new Text("java21days.com"); 15: Text ttlText = new Text("86400"); 16: 17: // Create a document using the domains root
element 18: Document doc = new Document(domains); 19: 20: // Add XML data to the root element 21: domains.appendChild(domain); 22: domain.appendChild(name); 23: name.appendChild(nameText); 24: domain.appendChild(dns); 25: dns.appendChild(ttl); 26: ttl.appendChild(ttlText); 27: dns.appendChild(ip); 28: ip.appendChild(""); 29: domain.appendChild(webdir); 30: webdir.appendChild("/web/java21days"); 31: webdir.addAttribute(status); 32: 33: // Display the XML document 34: System.out.println(doc.toXML()); 35: } 36: }

The Domains application displays the XML document it creates on standard output. The following command runs the application and redirects its output to a file called domains.xml:

java Domains > domains.xml

XOM automatically precedes a document with an XML declaration.

An important thing to note about the XML produced by this application is that it contains no indentation; elements are stacked on the same line.

XOM only preserves significant whitespace when representing XML data—the spaces between elements in Listing 20.6 are strictly for presentation purposes and are not produced automatically when XOM creates an XML document. A subsequent example demonstrates how to control indentation.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials