FileDocCategorySizeDatePackage
DocWriteDOM.javaAPI DocExample1797Mon Feb 11 09:27:04 GMT 2002None

DocWriteDOM

public class DocWriteDOM extends Object
Make up and write an XML document, using DOM UPDATED FOR JAXP.
author
Ian Darwin, ian@darwinsys.com
version
$Id: DocWriteDOM.java,v 1.4 2002/02/11 14:27:05 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;
		}