FileDocCategorySizeDatePackage
DocWriteDOM.javaAPI DocExample1912Sun Feb 29 21:42:58 GMT 2004None

DocWriteDOM

public class DocWriteDOM extends Object
Make up and write an XML document, using DOM UPDATED FOR JAXP.
author
Ian Darwin, http://www.darwinsys.com/
version
$Id: DocWriteDOM.java,v 1.6 2004/03/01 03:42:57 ian Exp $

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] av)

		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);
	
protected org.w3c.dom.DocumentmakeDoc()
Generate the XML document

		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;
		}