You
just need to use the Element.setAttribute() methods in Xerces, which are well
explained in the API.
Cory
-----Original Message-----
From: borg borg [mailto:[EMAIL PROTECTED]]
Sent: Thursday, June 14, 2001 1:42 PM
To: [EMAIL PROTECTED]
Subject: How to add attributesHiI'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
&nr 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 Serializerserial.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
