Hi
  I'm new to this so I was wondering if anyone could give me a hand.  I'm looking at the sample on creating an xml doc and trying to figure out how to add attributes to it.
This is the sample that came with the download.
 
I would like to do something like this:
 
<?xml version="1.0" encoding="UTF-8"?>
<person
  <name>Jeff</name>
  <age>8</age>
  <height>1.80</height>
</person>
 
But would like to add an attribute:
 
<?xml version="1.0" encoding="UTF-8"?>
<person
  <name value="35">Jeff</name>
  <age>8</age>
  <height>1.80</height>
</person>
 
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 );   file://Serialize DOM
            StringWriter  stringOut = new StringWriter();        file://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() ); file://Spit out DOM as a String
        } catch ( Exception ex ) {
            ex.printStackTrace();
        }
    }
}
 
 
Any ideas on how to add it to the existing source?
 
Thanks


Get your FREE download of MSN Explorer at http://explorer.msn.com

Reply via email to