Home arrow Java & J2EE arrow Page 12 - Integrating XML with J2EE

Modifying a DOM Tree - Java

In this article, Martin Bond discusses XML and its associated APIs and standards, and how XML can be used to create flexible structured data that is inherently portable. This excerpt is from chapter (Day) 16 of Teach Yourself J2EE in 21 Days, second edition, by Martin Bond, et. al. (Sams, ISBN: 0672325586)

TABLE OF CONTENTS:
  1. Integrating XML with J2EE
  2. Benefits and Characteristics of XML
  3. Structure and Syntax of XML
  4. Structure of an XML Document
  5. Well-formed XML Documents
  6. Namespaces
  7. Element Type Declarations
  8. XML Schemas
  9. Parsing XML
  10. Parsing XML Using SAX
  11. Document Object Model (DOM) Parser
  12. Modifying a DOM Tree
  13. Java Architecture for XML Binding
  14. Summary
By: Sams Publishing
Rating: starstarstarstarstar / 23
November 16, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

We will now look at using the DOM API—to modify the contents or structure of the XML. Unlike SAX, DOM provides a number of methods that allow nodes to be added, deleted, changed, or replaced in the DOM tree. Table 16.10 summarizes these methods.

Table 16.10 Document Interface Methods to Inspect DOM Nodes

Method Name

Description

appendChild(Node newNode)

Adds the new node to the end of the NodeList of children of this node.

cloneNode(boolean deep)

Returns a duplicate of a node. The cloned node has no parent. If deep is true, the whole tree below this node is cloned; if false, only the node itself is cloned.

insertBefore(Node newNode, Node refNode)

Inserts the newNode before the existing refNode.

removeChild(Node oldNode)

Removes the oldNode from the list of children.

replaceChild(Node newNode, Node oldNode)

Replaces the oldNode with newNode in the child NodeList.

setNodeValue(String nodeValue)

Set the value of this node, depending on its type.

setPrefix(java.lang.String prefix)

Set the namespace prefix of this node.


For example, the following code fragment simply creates a new customer element and appends it to the end of the XML document:

Node newNode = addXMLNode (document, "Customer", "Columbus");
Element root = document.getDocumentElement();
root.appendChild(newNode);
private static Node addXMLNode (Document document, String name, String text) {
Element e = document.createElement(name);
Text t = document.createTextNode(text);
e.appendChild (t);
return e;
}

The following XML element is added to the XML file that is read in by this example code:

<customer>Columbus</customer>

Outputting a DOM Tree

Having parsed or created an XML document in memory, a common requirement is to output the DOM tree. The javax.xml.transform class defines a transformer that can be used to output a DOM tree from memory. The following code shows how easy it is to take a DOM tree and output it to the screen:

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(new DOMSource(root), new StreamResult(System.out));

Note - In Day 17 you will see how to use XML Sylesheets with the transformer object to format the transformed output.


A Simple DOM Example

The WebDDBuilder example shown in Listing 16.10 is a simple program that creates a new Web Application deployment descriptor and adds a single <servlet> and <servlet-mapping> element to the tree before writing the updated DD. The Web Application DD was described on Day 12, "Servlets."

Listing 16.10 WebDDBuilder.java

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.xml.sax.*;
import java.io.*;
import org.w3c.dom.*;
import java.util.*;
public class WebDDBuilder {
public static void main(String argv[]) {
int argCount = argv.length;
if (argCount != 2) {
System.err.println("Usage: WebDDBuilder servlet-class URL-mapping");
System.exit(1);
}
String servletClass = argv[0];
String URLPattern = argv[1];
try {
WebDDBuilder dd = new WebDDBuilder();
dd.addServlet(servletClass, URLPattern);
// output document
dd.print(System.out);
}
catch (IllegalArgumentException ex) {
System.err.println ("Invalid argument" + ex);
ex.printStackTrace(System.out);
}
}
private static final String SERVLET_VERSION = "2.4";
private static final String XML_NAMESPACE = 
"http://java.sun.com/xml/ns/j2ee";
private static final String XML_SCHEMA_INST = 
"http://www.w3.org/2001/XMLSchema-instance";
private static final String XML_SCHEMA_LOC = 
"http://java.sun.com/xml/ns/j2ee
_ http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd";
private static final String SERVLET = "servlet";
private static final String SERVLET_MAPPING = "servlet-mapping";
private static final String SERVLET_NAME = "servlet-name";
private static final String SERVLET_CLASS = "servlet-class";
private static final String URL_PATTERN = "url-pattern";
private static final String[] DD_ELEMENTS = {"icon", "display-name",
"description", "distributable", "context-param", "filter",
"filter-mapping", "listener", "servlet", "servlet-mapping",
"session-config", "mimemapping", "welcome-file-list", "error-page",
"taglib", "resource-env-ref", "resource-ref", "security-constraint",
"login-config", "security-role", "env-entry", 
"ejb-ref", "ejb-local-ref" };
private Document document;
private Element root;
private HashMap DDElements; 
public WebDDBuilder () {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument();
root = document.createElement("web-app");
root.setAttribute("version", SERVLET_VERSION);
root.setAttribute("xmlns", XML_NAMESPACE);
root.setAttribute("xmlns:xsi", XML_SCHEMA_INST);
root.setAttribute("xsi:schemaLocation", XML_SCHEMA_LOC);
DDElements = createDDMap(DD_ELEMENTS);
}
catch (ParserConfigurationException ex) {
System.err.println ("Failed to create DOM document:" + ex);
}
}
private void addServlet (String servletClass, String URLPattern) {
//create the servlet name from the servlet class name
// if fully qualified class name take just last part
int index = servletClass.lastIndexOf(".");
String servletName;
if (index != -1)
servletName = servletClass.substring(index+1);
else
servletName = servletClass;
// build the servlet element
Element servlet_name = document.createElement(SERVLET_NAME);
servlet_name.appendChild(document.createTextNode(servletName));
Element servlet_class = document.createElement(SERVLET_CLASS);
servlet_class.appendChild(document.createTextNode(servletClass));
Element servlet = document.createElement (SERVLET);
servlet.appendChild(servlet_name);
servlet.appendChild(servlet_class);
// find where in the DOM to insert the new servlet node
Node refChild = findNode (root, DDElements, SERVLET);
root.insertBefore(servlet, refChild);
// build the servlet-mapping element
Element url_pattern = document.createElement(URL_PATTERN);
url_pattern.appendChild(document.createTextNode(URLPattern));
Element servlet_mapping = document.createElement (SERVLET_MAPPING);
// no need to create servlet name element as we already have one
// make sure we clone deep so that we get the text node
servlet_mapping.appendChild(servlet_name.cloneNode(true));
servlet_mapping.appendChild(url_pattern);
refChild = findNode (root, DDElements, SERVLET_MAPPING);
root.insertBefore(servlet_mapping, refChild);
}
private void print (PrintStream stream) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
transformer.transform(new DOMSource(root), 
new StreamResult(stream));
}
catch (TransformerConfigurationException ex) {
System.err.println ("Failed to create transformer factory:" + ex);
}
catch (TransformerException ex) {
System.err.println ("Failed to transform DOM tree:" + ex);
}
}
private Node findNode (Node treeRoot, HashMap ddSchema, String tagName) {
// find out index of tagName
int refKey = getKey (ddSchema, tagName);
NodeList tags = treeRoot.getChildNodes();
int tagsLen = (tags != null) ? tags.getLength() : 0;
// find first tag after tagName in tree
for (int i = 0; i < tagsLen; i++) {
Node tag = tags.item(i);
if (getKey(ddSchema, tag.getNodeName()) > refKey)
return tag;
}
return null;
}
private int getKey (HashMap ddSchema, String tagName) {
for (int key = 0; key < ddSchema.size(); key++) {
if (ddSchema.get(new Integer(key)).equals(tagName))
return key;
}
return -1; 
}
private HashMap createDDMap(String[] ddSchema) {
HashMap map = new HashMap();
for (int i = 0; i < ddSchema.length; i++)
map.put(new Integer(i), ddSchema[i]);
return map;
}

The WebDDBuilder example in Listing 16.10 starts by creating a new, empty, DOM tree representing an empty Web Application DD. Next the addServlet() method is called to add the servlet name and URL pattern passed as command-line parameters.

The addServlet()method builds two XML DD elements, <servlet> and <servlet-mapping>, using the values supplied as arguments. Each of these elements has a <servlet-name> sub-element, so instead of creating a new element from scratch, addServlet uses the Node.cloneNode() method to create a copy. A deep copy is preformed by passing true as the parameter to cloneNode; this ensures that all the child nodes are also cloned.

Finally, the print() method is called to output the DOM tree using a Transformer object.

As with the SAX example, this code does not use any J2EE components; you can simply compile and run it from the command line. From the Day16/examples directory run the command

> java –classpath classes demo.Hello /hello

This will create a DD entry for the demo.servlet with URL pattern /hello. Alternatively, use the supplied asant build files and enter

> asant WebDDBuilder

Provide the servlet name and URL pattern when prompted:

The resultant DD looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
_ http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>demo.Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>

This chapter is from Teach Yourself J2EE in 21 Days, second edition, by Martin Bond et. al. (Sams, 2004, ISBN: 0-672-32558-6). Check it out at your favorite bookstore today. Buy this book now.



 
 
>>> More Java & J2EE Articles          >>> More By Sams Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: