* @version $Id: DOMGenerate.java,v 1.4 2001/08/23 00:35:17 lehors Exp $
*/
public class DOMGenerate {
public static void main( String[] argv ) {
try {
Document doc= new DocumentImpl();
Element root = doc.createElement("person"); // Create Root Element
Element item = doc.createElement("name"); // Create element
item.appendChild( doc.createTextNode("Jeff") );
root.appendChild( item ); // atach element to Root element
item = doc.createElement("age"); // Create another Element
item.appendChild( doc.createTextNode("28" ) );
root.appendChild( item ); // Attach Element to previous element down tree
item = doc.createElement("height");
item.appendChild( doc.createTextNode("1.80" ) );
root.appendChild( item ); // Attach another Element - grandaugther
doc.appendChild( root ); // Add Root to Document
OutputFormat format = new OutputFormat( doc ); //Serialize DOM
StringWriter stringOut = new StringWriter(); //Writer will be a String
XMLSerializer serial = new XMLSerializer( stringOut, format );
serial.asDOMSerializer(); // As a DOM Serializer
serial.serialize( doc.getDocumentElement() );
System.out.println( "STRXML = " + stringOut.toString() ); //Spit out DOM as a String
} catch ( Exception ex ) {
ex.printStackTrace();
}