DOM 程序
package dom;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class DomTest {
private Document doc = null;
public DomTest( String xmlFile ) throws Exception {
this.dingWei( xmlFile );
}
private void dingWei( String xmlFile ) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
db = dbf.newDocumentBuilder();
this.doc = db.parse( xmlFile );
}
public Element getRoot() {
return doc.getDocumentElement();
}
public NodeList getChildList( Element element, String childElementName ) {
return element.getElementsByTagName( childElementName );
}
public String getValue( Element element ) {
return ( ( ( Text )element.getFirstChild() ).getNodeValue() )
.toString();
}
public String getAttribute( Element element, String attributeName ) {
return element.getAttribute( attributeName );
}
/**