Recipe 21.6 Generating Your Own XML with DOM


You want to generate your own XML files or modify existing documents.


Use DOM or JDOM; parse or create the document and call its write method.


Sun's XmlDocument class has a write( ) method that can be called with either an OutputStream or a Writer. To use it, create an XML document object using the XmlDocument constructor. Create nodes, append them into the tree, and then call the document's write( ) method. For example, suppose you want to generate a poem in XML. Running the program and letting the XML appear on the standard output might look something like this:

$ java DocWrite <?xml version="1.0" encoding="UTF-8"?> <Poem>   <Stanza>     <Line>Once, upon a midnight dreary</Line>     <Line>While I pondered, weak and weary</Line>   </Stanza> </Poem> $

The code for this is fairly short; see Example 21-9 for the code using DOM. Code for using JDOM is similar; see in the online source code.

Example 21-9.
import; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; /** Make up and write an XML document, using DOM  * UPDATED FOR JAXP.  * @author Ian Darwin,  * @version $Id: ch21.xml,v 1.5 2004/05/04 20:13:38 ian Exp $  */ public class DocWriteDOM {     public static void main(String[] av) throws IOException {         DocWriteDOM dw = new DocWriteDOM( );         Document doc = dw.makeDoc( );         // Sadly, the write( ) method is not in the DOM spec, so we         // have to cast the Document to its implementing class         // in order to call the Write method.         //         // WARNING         //         // This code therefore depends upon the particular         // parser implementation.         //         ((org.apache.crimson.tree.XmlDocument)doc).write(System.out);     }     /** Generate the XML document */     protected Document makeDoc( ) {         try {             DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance( );             DocumentBuilder parser = fact.newDocumentBuilder( );             Document doc = parser.newDocument( );             Node root = doc.createElement("Poem");             doc.appendChild(root);             Node stanza = doc.createElement("Stanza");             root.appendChild(stanza);                          Node line = doc.createElement("Line");             stanza.appendChild(line);             line.appendChild(doc.createTextNode("Once, upon a midnight dreary"));             line = doc.createElement("Line");             stanza.appendChild(line);             line.appendChild(doc.createTextNode("While I pondered, weak and weary"));             return doc;         } catch (Exception ex) {             System.err.println("+============================+");             System.err.println("|        XML Error           |");             System.err.println("+============================+");             System.err.println(ex.getClass( ));             System.err.println(ex.getMessage( ));             System.err.println("+============================+");             return null;         }     } }

A more complete program would create an output file and have better error reporting. It would also have more lines of the poem than I can remember.

Sun's XmlDocument class is not a committed part of the standard, which is why the code casts the object to org.apache.crimson.tree.XmlDocument before calling its write method. However, other vendors' APIs have similar functionality.

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: