Use DOM to resolve and print XML documents

xiaoxiao2021-03-06  40

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 (" / n "); return header.tostring ();} princebuffer circle (Nodelist List, StringBuffer SB) {// Removable call, get all Element and Node. For (int i = 0; i "); // Get all Attribute of the Element. SB = this.circle (Node.GetChildNodes (), SB); // Removable call, operate the child's child node. SB.Append ("");} else if (node.getnodettype () == node.comment_node) {// Check this non-Element node is comment. Sb.Append ("");} else {sb.append (node.getnodevalue ());}} returnch sb;}}} return sb;} private string parsexml ) {// parsing an XML document.

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 ("

K: /TEST11/XML/Test.xml

adfadfadsdsafsda DOM1 doms.dom1 name bitan doms.listenerclass tag1.tld /web-inf/tlds/tag1.tld Below is some information: January 14, 2005 J2SDK 1.5.0_01 UltraEdit-32 test ends. Result:

转载请注明原文地址:https://www.9cbs.com/read-68729.html

New Post(0)