During the API document, I found that in the DOM, a comment, an attribute, a line text, an element (Element) is a Node. Node Node Node is called an element (ELEMENT).
Therefore, the most important thing to process the XML document is to resolve and process a variety of different NODEs.
Dom6.java
Package DOMS;
Import javax.xml.parsrs. *; // This package is included in the J2EE 1.4.2 API. Import org.w3c.dom. *; // This package is included in the J2SE 1.5.0 API. Import org.xml.sax. *; // This package is included in the J2EE 1.5.0 API.
Public class dom6 {public static void main (string [] args) {DOM6 D6 = New DOM6 (); system.out.println (D6.Parsexml ("k: /test11/xml/test.xml");}
Private string getDocumentType (Document Doc) {// Gets DocumentType information. For simplicity, it does not involve parsing of Entity et al. StringBuffer sb = new StringBuffer (); DocumentType type = doc.getDoctype (); String name = type.getName (); String publicId = type.getPublicId (); String systemId = type.getSystemId (); if (name == null "Return" "; sb.append (" / n"); return sb.tostring ();} private string getElementattrs (node node) {// Get one All Attribute of Element. StringBuffer Attrs = new stringbuffer (); if (node.haschildnodes ()) {NamedNodeMap Map = node.getattributes (); for (int i = 0; i Header.Append (" xml"); Header.Append ("Version = /" Version "/"); Header.Append ("Encoding = /" Encoding "/"); Header .append (Standalone == false? ":" Standalone = / "true /" "); header.append ("?> / n "); return header.tostring ();} princebuffer circle (Nodelist List, StringBuffer SB) {// Removable call, get all Element and Node. For (int i = 0; i StringBuffer sb = new StringBuffer ( "/ ParseXML /// n"); try {javax.xml.parsers.DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder (); org. w3c.dom.document doc = builder.parse (XMLFile); // Get an XML document. Sb.Append (this.GetxmlHeader (DOC)); // Get an XML header definition. Sb.Append (this.getDocumentType (DOC)); // Gets DocumentType information. Org.w3c.dom.Element RootElement = doc.getdocumentElement (); // Get root elements. String roottagname = rootelement.getagname (); sb.append ("<" roottagname this.getlementattrs (rootElement) ">"); if (RootElement.HaschildNodes ()) {// Generally, the root element must exist Child node. SB = this.circle (RootElement.getChildNodes (), SB); // recursive call, the child node of the root element. } Sb.append ("" "} catch (javax.xml.parsers.ParserConfigurationException PCE) {} catch (org.xml.sax.saxexception se) {} catch (java.io. IOEXCEPTION IOE) {} return sb.tostring ();}} K: /TEST11/XML/Test.xml XML Version = "1.0" encoding = "GBK"?>