The call method of the DOM seems to be similar to SAX:
/ ** /
/ * * CREATED ON 2005-2-24 * All rights reserved. * * /
package sean.home.test; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node;
/ ** /
/ ** * @Author sean gao *
* gaoyuxiang@gmail.com * p> * * /
public
Class
Domtest
{Public static void main (String [] args) throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); Document myDocument = builder.parse (new File (args [0])); Node Root = myDocument.getdocument (); // We already have a complete reflection of the tree structure that processes the XML document and obtains its root node system.out.println (root.getnodename ()); // }
Here, we have achieved the desired DOM model, then we can traverse or operate it. For ORG.W3C.DOM.DOCUMENT types, we can call the following common methods:
GetDocumentelement () getElementsBytagname (String) getChildNodes () getParentNode () getFirstChild () getLastchild () getPrevioussibling ()
After getting the org.w3c.dom.node object, we can:
GetNodEname () getnodEvaluri () getnodespaceuri () getnodespaceuri () getNamespaceuri () getnamespaceuri () Hasattributes () HasChildNodes ()
These methods know what to do from the name, right? I will explain it.