search
HomeBackend DevelopmentXML/RSS TutorialDetailed introduction to the sample code of four methods of parsing Xml

Keywords: Java parsing xml, four methods of parsing xml, DOM, SAX, JDOM, DOM4j, XPath

[Introduction]

Technology currently used to parse XML in Java There are many, the mainstream ones include DOM, SAX, JDOM, and DOM4j. The following mainly introduces the use, advantages, disadvantages, and performance tests of these four XML document parsing technologies.

1. [Basic Knowledge - Literacy]

sax and dom are two methods for parsing xml documents (no specific implementation, just interfaces), so they alone cannot parse xml Document; jaxp is just an API, which further encapsulates the sax and dom interfaces, and provides DomcumentBuilderFactory/DomcumentBuilder and SAXParserFactory/SAXParser (the xerces interpreter is used by default).

2. [Simple introduction to using DOM, SAX, JDOM, and DOM4j]

1. [DOM (Document Object Model)]
By W3C Provides an interface that reads the entire XML document into memory and builds a DOM tree to operate each node (Node).
Sample code:

<?xml version="1.0" encoding="UTF-8"?>  
<university name="pku">  
    <college name="c1">  
        <class name="class1">  
            <student name="stu1" sex=&#39;male&#39; age="21" />  
            <student name="stu2" sex=&#39;female&#39; age="20" />  
            <student name="stu3" sex=&#39;female&#39; age="20" />  
        </class>  
        <class name="class2">  
            <student name="stu4" sex=&#39;male&#39; age="19" />  
            <student name="stu5" sex=&#39;female&#39; age="20" />  
            <student name="stu6" sex=&#39;female&#39; age="21" />  
        </class>  
    </college>  
    <college name="c2">  
        <class name="class3">  
            <student name="stu7" sex=&#39;male&#39; age="20" />  
        </class>  
    </college>  
    <college name="c3">  
    </college>  
</university>

text.xml is used in the following code (the document is placed in the src path, and is compiled in the classes path), which refers to the xml document.

package test.xml;  
import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import javax.xml.parsers.DocumentBuilder;  
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserConfigurationException;  
import javax.xml.transform.Transformer;  
import javax.xml.transform.TransformerConfigurationException;  
import javax.xml.transform.TransformerException;  
import javax.xml.transform.TransformerFactory;  
import javax.xml.transform.dom.DOMSource;  
import javax.xml.transform.stream.StreamResult;  
import org.w3c.dom.Document;  
import org.w3c.dom.Element;  
import org.w3c.dom.Node;  
import org.w3c.dom.NodeList;  
import org.w3c.dom.Text;  
import org.xml.sax.SAXException;  
/** 
 * dom读写xml 
 * @author whwang 
 */  
public class TestDom {  
    public static void main(String[] args) {  
        read();  
        //write();   
    }  
    public static void read() {  
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
        try {  
            DocumentBuilder builder = dbf.newDocumentBuilder();  
            InputStream in = TestDom.class.getClassLoader().getResourceAsStream("test.xml");  
            Document doc = builder.parse(in);  
            // root <university>   
            Element root = doc.getDocumentElement();  
            if (root == null) return;  
            System.err.println(root.getAttribute("name"));  
            // all college node   
            NodeList collegeNodes = root.getChildNodes();  
            if (collegeNodes == null) return;  
            for(int i = 0; i < collegeNodes.getLength(); i++) {  
                Node college = collegeNodes.item(i);  
                if (college != null && college.getNodeType() == Node.ELEMENT_NODE) {  
                    System.err.println("\t" + college.getAttributes().getNamedItem("name").getNodeValue());  
                    // all class node   
                    NodeList classNodes = college.getChildNodes();  
                    if (classNodes == null) continue;  
                    for (int j = 0; j < classNodes.getLength(); j++) {  
                        Node clazz = classNodes.item(j);  
                        if (clazz != null && clazz.getNodeType() == Node.ELEMENT_NODE) {  
                            System.err.println("\t\t" + clazz.getAttributes().getNamedItem("name").getNodeValue());  
                            // all student node   
                            NodeList studentNodes = clazz.getChildNodes();  
                            if (studentNodes == null) continue;  
                            for (int k = 0; k < studentNodes.getLength(); k++) {  
                                Node student = studentNodes.item(k);  
                                if (student != null && student.getNodeType() == Node.ELEMENT_NODE) {  
                                    System.err.print("\t\t\t" + student.getAttributes().getNamedItem("name").getNodeValue());  
                                    System.err.print(" " + student.getAttributes().getNamedItem("sex").getNodeValue());  
                                    System.err.println(" " + student.getAttributes().getNamedItem("age").getNodeValue());  
                                }  
                            }  
                        }  
                    }  
                }  
            }  
        } catch (ParserConfigurationException e) {  
            e.printStackTrace();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (SAXException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
    public static void write() {  
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
        try {  
            DocumentBuilder builder = dbf.newDocumentBuilder();  
            InputStream in = TestDom.class.getClassLoader().getResourceAsStream("test.xml");  
            Document doc = builder.parse(in);  
            // root <university>   
            Element root = doc.getDocumentElement();  
            if (root == null) return;  
            // 修改属性   
            root.setAttribute("name", "tsu");  
            NodeList collegeNodes = root.getChildNodes();  
            if (collegeNodes != null) {  
                for (int i = 0; i <collegeNodes.getLength() - 1; i++) {  
                    // 删除节点   
                    Node college = collegeNodes.item(i);  
                    if (college.getNodeType() == Node.ELEMENT_NODE) {  
                        String collegeName = college.getAttributes().getNamedItem("name").getNodeValue();  
                        if ("c1".equals(collegeName) || "c2".equals(collegeName)) {  
                            root.removeChild(college);  
                        } else if ("c3".equals(collegeName)) {  
                            Element newChild = doc.createElement("class");  
                            newChild.setAttribute("name", "c4");  
                            college.appendChild(newChild);  
                        }  
                    }  
                }  
            }  
            // 新增节点   
            Element addCollege = doc.createElement("college");  
            addCollege.setAttribute("name", "c5");  
            root.appendChild(addCollege);  
            Text text = doc.createTextNode("text");  
            addCollege.appendChild(text);  
            // 将修改后的文档保存到文件   
            TransformerFactory transFactory = TransformerFactory.newInstance();  
            Transformer transFormer = transFactory.newTransformer();  
            DOMSource domSource = new DOMSource(doc);  
            File file = new File("src/dom-modify.xml");  
            if (file.exists()) {  
                file.delete();  
            }  
            file.createNewFile();  
            FileOutputStream out = new FileOutputStream(file);           
            StreamResult xmlResult = new StreamResult(out);  
            transFormer.transform(domSource, xmlResult);  
            System.out.println(file.getAbsolutePath());  
        } catch (ParserConfigurationException e) {  
            e.printStackTrace();  
        } catch (SAXException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (TransformerConfigurationException e) {  
            e.printStackTrace();  
        } catch (TransformerException e) {  
            e.printStackTrace();  
        }  
    }  
}


##This code can be changed with just a slight modification It is more concise, and there is no need to write if all the time to determine whether there are child nodes.



2. [SAX (Simple API for XML)]SAX does not need to load the entire document into memory, it is based on
eventdriver API (Observer mode), users only need to register the events they are interested in. SAX provides EntityResolver, DTDHandler, ContentHandler, and ErrorHandler interfaces, which are used to monitor parsing entity events, DTD processing events, text processing events, and processing error events respectively, similar to AWT. SAX also provides a default class DefaultHandler for these four interfaces (the default implementation here is actually an empty method). Generally, you only need to inherit DefaultHandler and rewrite the events you are interested in. Sample code:

package test.xml;  
import java.io.IOException;  
import java.io.InputStream;  
import javax.xml.parsers.ParserConfigurationException;  
import javax.xml.parsers.SAXParser;  
import javax.xml.parsers.SAXParserFactory;  
import org.xml.sax.Attributes;  
import org.xml.sax.InputSource;  
import org.xml.sax.Locator;  
import org.xml.sax.SAXException;  
import org.xml.sax.SAXParseException;  
import org.xml.sax.helpers.DefaultHandler;  
/** 
 * 
 * @author whwang 
 */  
public class TestSAX {  
    public static void main(String[] args) {  
        read();  
        write();  
    }  
    public static void read() {  
        try {  
            SAXParserFactory factory = SAXParserFactory.newInstance();  
            SAXParser parser = factory.newSAXParser();  
            InputStream in = TestSAX.class.getClassLoader().getResourceAsStream("test.xml");  
            parser.parse(in, new MyHandler());  
        } catch (ParserConfigurationException e) {  
            e.printStackTrace();  
        } catch (SAXException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
    public static void write() {  
        System.err.println("纯SAX对于写操作无能为力");  
    }  
}  
// 重写对自己感兴趣的事件处理方法   
class MyHandler extends DefaultHandler {  
    @Override  
    public InputSource resolveEntity(String publicId, String systemId)  
            throws IOException, SAXException {  
        return super.resolveEntity(publicId, systemId);  
    }  
    @Override  
    public void notationDecl(String name, String publicId, String systemId)  
            throws SAXException {  
        super.notationDecl(name, publicId, systemId);  
    }  
    @Override  
    public void unparsedEntityDecl(String name, String publicId,  
            String systemId, String notationName) throws SAXException {  
        super.unparsedEntityDecl(name, publicId, systemId, notationName);  
    }  
    @Override  
    public void setDocumentLocator(Locator locator) {  
        super.setDocumentLocator(locator);  
    }  
    @Override  
    public void startDocument() throws SAXException {  
        System.err.println("开始解析文档");  
    }  
    @Override  
    public void endDocument() throws SAXException {  
        System.err.println("解析结束");  
    }  
    @Override  
    public void startPrefixMapping(String prefix, String uri)  
            throws SAXException {  
        super.startPrefixMapping(prefix, uri);  
    }  
    @Override  
    public void endPrefixMapping(String prefix) throws SAXException {  
        super.endPrefixMapping(prefix);  
    }  
    @Override  
    public void startElement(String uri, String localName, String qName,  
            Attributes attributes) throws SAXException {  
        System.err.print("Element: " + qName + ", attr: ");  
        print(attributes);  
    }  
    @Override  
    public void endElement(String uri, String localName, String qName)  
            throws SAXException {  
        super.endElement(uri, localName, qName);  
    }  
    @Override  
    public void characters(char[] ch, int start, int length)  
            throws SAXException {  
        super.characters(ch, start, length);  
    }  
    @Override  
    public void ignorableWhitespace(char[] ch, int start, int length)  
            throws SAXException {  
        super.ignorableWhitespace(ch, start, length);  
    }  
    @Override  
    public void processingInstruction(String target, String data)  
            throws SAXException {  
        super.processingInstruction(target, data);  
    }  
    @Override  
    public void skippedEntity(String name) throws SAXException {  
        super.skippedEntity(name);  
    }  
    @Override  
    public void warning(SAXParseException e) throws SAXException {  
        super.warning(e);  
    }  
    @Override  
    public void error(SAXParseException e) throws SAXException {  
        super.error(e);  
    }  
    @Override  
    public void fatalError(SAXParseException e) throws SAXException {  
        super.fatalError(e);  
    }  
    private void print(Attributes attrs) {  
        if (attrs == null) return;  
        System.err.print("[");  
        for (int i = 0; i < attrs.getLength(); i++) {  
            System.err.print(attrs.getQName(i) + " = " + attrs.getValue(i));  
            if (i != attrs.getLength() - 1) {  
                System.err.print(", ");  
            }  
        }  
        System.err.println("]");  
    }  
}

3. [JDOM]JDOM is very similar to DOM. It is a pure JAVA API that processes XML. The API uses the Collections class extensively, and JDOM only uses concrete classes and not interfaces. JDOM itself does not contain a parser. It typically uses a SAX2 parser to parse and validate input XML documents (although it can also take previously constructed DOM representations as input). It contains converters to output JDOM representations into SAX2 event streams, DOM
Models, or XML text documentsSample code:

package test.xml;  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.List;  
import org.jdom.Attribute;  
import org.jdom.Document;  
import org.jdom.Element;  
import org.jdom.JDOMException;  
import org.jdom.input.SAXBuilder;  
import org.jdom.output.XMLOutputter;  
/** 
 * JDom读写xml 
 * @author whwang 
 */  
public class TestJDom {  
    public static void main(String[] args) {  
        //read();   
        write();  
    }  
    public static void read() {  
        try {  
            boolean validate = false;  
            SAXBuilder builder = new SAXBuilder(validate);  
            InputStream in = TestJDom.class.getClassLoader().getResourceAsStream("test.xml");  
            Document doc = builder.build(in);  
            // 获取根节点 <university>   
            Element root = doc.getRootElement();  
            readNode(root, "");  
        } catch (JDOMException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
    @SuppressWarnings("unchecked")  
    public static void readNode(Element root, String prefix) {  
        if (root == null) return;  
        // 获取属性   
        List<Attribute> attrs = root.getAttributes();  
        if (attrs != null && attrs.size() > 0) {  
            System.err.print(prefix);  
            for (Attribute attr : attrs) {  
                System.err.print(attr.getValue() + " ");  
            }  
            System.err.println();  
        }  
        // 获取他的子节点   
        List<Element> childNodes = root.getChildren();  
        prefix += "\t";  
        for (Element e : childNodes) {  
            readNode(e, prefix);  
        }  
    }  
    public static void write() {  
        boolean validate = false;  
        try {  
            SAXBuilder builder = new SAXBuilder(validate);  
            InputStream in = TestJDom.class.getClassLoader().getResourceAsStream("test.xml");  
            Document doc = builder.build(in);  
            // 获取根节点 <university>   
            Element root = doc.getRootElement();  
            // 修改属性   
            root.setAttribute("name", "tsu");  
            // 删除   
            boolean isRemoved = root.removeChildren("college");  
            System.err.println(isRemoved);  
            // 新增   
            Element newCollege = new Element("college");  
            newCollege.setAttribute("name", "new_college");  
            Element newClass = new Element("class");  
            newClass.setAttribute("name", "ccccc");  
            newCollege.addContent(newClass);  
            root.addContent(newCollege);  
            XMLOutputter out = new XMLOutputter();  
            File file = new File("src/jdom-modify.xml");  
            if (file.exists()) {  
                file.delete();  
            }  
            file.createNewFile();  
            FileOutputStream fos = new FileOutputStream(file);  
            out.output(doc, fos);  
        } catch (JDOMException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}

4, [DOM4j]dom4j is currently the best in xml parsing (Hibernate and Sun's JAXM also use dom4j to parse XML). It incorporates many functions beyond basic XML document representation, including integrated XPath support, XML Schema Support and event-based processing for large documents or streaming documents
Sample code:

package test.xml;  
import java.io.File;  
import java.io.FileWriter;  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.List;  
import org.dom4j.Attribute;  
import org.dom4j.Document;  
import org.dom4j.DocumentException;  
import org.dom4j.DocumentHelper;  
import org.dom4j.Element;  
import org.dom4j.ProcessingInstruction;  
import org.dom4j.VisitorSupport;  
import org.dom4j.io.SAXReader;  
import org.dom4j.io.XMLWriter;  
/** 
 * Dom4j读写xml 
 * @author whwang 
 */  
public class TestDom4j {  
    public static void main(String[] args) {  
        read1();  
        //read2();   
        //write();   
    }  
    public static void read1() {  
        try {  
            SAXReader reader = new SAXReader();  
            InputStream in = TestDom4j.class.getClassLoader().getResourceAsStream("test.xml");  
            Document doc = reader.read(in);  
            Element root = doc.getRootElement();  
            readNode(root, "");  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        }  
    }  
    @SuppressWarnings("unchecked")  
    public static void readNode(Element root, String prefix) {  
        if (root == null) return;  
        // 获取属性   
        List<Attribute> attrs = root.attributes();  
        if (attrs != null && attrs.size() > 0) {  
            System.err.print(prefix);  
            for (Attribute attr : attrs) {  
                System.err.print(attr.getValue() + " ");  
            }  
            System.err.println();  
        }  
        // 获取他的子节点   
        List<Element> childNodes = root.elements();  
        prefix += "\t";  
        for (Element e : childNodes) {  
            readNode(e, prefix);  
        }  
    }  
    public static void read2() {  
        try {  
            SAXReader reader = new SAXReader();  
            InputStream in = TestDom4j.class.getClassLoader().getResourceAsStream("test.xml");  
            Document doc = reader.read(in);  
            doc.accept(new MyVistor());  
        } catch (DocumentException e) {  
            e.printStackTrace();  
        }  
    }  
    public static void write() {  
        try {  
            // 创建一个xml文档   
            Document doc = DocumentHelper.createDocument();  
            Element university = doc.addElement("university");  
            university.addAttribute("name", "tsu");  
            // 注释   
            university.addComment("这个是根节点");  
            Element college = university.addElement("college");  
            college.addAttribute("name", "cccccc");  
            college.setText("text");  
            File file = new File("src/dom4j-modify.xml");  
            if (file.exists()) {  
                file.delete();  
            }  
            file.createNewFile();  
            XMLWriter out = new XMLWriter(new FileWriter(file));  
            out.write(doc);  
            out.flush();  
            out.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  
class MyVistor extends VisitorSupport {  
    public void visit(Attribute node) {  
        System.out.println("Attibute: " + node.getName() + "="  
                + node.getValue());  
    }  
    public void visit(Element node) {  
        if (node.isTextOnly()) {  
            System.out.println("Element: " + node.getName() + "="  
                    + node.getText());  
        } else {  
            System.out.println(node.getName());  
        }  
    }  
    @Override  
    public void visit(ProcessingInstruction node) {  
        System.out.println("PI:" + node.getTarget() + " " + node.getText());  
    }  
}

3. [Performance Test]

Environment: AMD4400+ 2.0 +GHz main frequency JDK6.0

Running parameters: -Xms400m -Xmx400m
xml file size: 10.7M
Result:
DOM: >581297ms
SAX: 8829ms
JDOM: 581297ms
DOM4j: 5309ms
The time includes IO, just a simple test,
for reference only! ! ! !

4. [Comparison]

1. [DOM] DOM is a tree-based structure, which usually requires loading the entire document and constructing the DOM tree , and then you can start working.
Advantages:
a. Since the entire tree is in memory, the xml document can be randomly accessed
b. The xml document can be modified
c. DOM is easier to use than SAX .
Disadvantages:
a. The entire document must be parsed at one time
a. Since the entire document needs to be loaded into memory, the cost is high for large documents

2. [SAX] SAX is similar to streaming media. It is event-driven, so there is no need to load the entire document into memory. Users only need to listen to the events they are interested in.
Advantages:
a. There is no need to load the entire xml document into memory, so it consumes less memory.
b. Multiple ContentHandlers can be registered.
Disadvantages:
a. Unable to randomly access the contents of xml Node
b. The document cannot be modified

3. [JDOM]JDOM is a pure Java API for processing XML. The Collections class is widely used in its API.
Advantages:
a. Advantages of DOM method
b. Java rules with SAX
Disadvantages
a. Disadvantages of DOM method

4. [DOM4J]These 4 Among all the XML parsing methods, it is the best one, combining ease of use and performance.


五、【小插曲XPath】

XPath 是一门在 XML 文档中查找信息的语言, 可用来在 XML 文档中对元素和属性进行遍历。XPath 是 W3C XSLT 标准的主要元素,并且 XQueryXPointer 同时被构建于 XPath 表达之上。因此,对 XPath 的理解是很多高级 XML 应用的基础。
XPath非常类似对数据库操作的SQL语言,或者说JQuery,它可以方便开发者抓起文档中需要的东西。(dom4j也支持xpath)
示例代码:

package test.xml;  
import java.io.IOException;  
import java.io.InputStream;  
import javax.xml.parsers.DocumentBuilder;  
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserConfigurationException;  
import javax.xml.xpath.XPath;  
import javax.xml.xpath.XPathConstants;  
import javax.xml.xpath.XPathExpression;  
import javax.xml.xpath.XPathExpressionException;  
import javax.xml.xpath.XPathFactory;  
import org.w3c.dom.Document;  
import org.w3c.dom.NodeList;  
import org.xml.sax.SAXException;  
public class TestXPath {  
    public static void main(String[] args) {  
        read();  
    }  
    public static void read() {  
        try {  
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
            DocumentBuilder builder = dbf.newDocumentBuilder();  
            InputStream in = TestXPath.class.getClassLoader().getResourceAsStream("test.xml");  
            Document doc = builder.parse(in);  
            XPathFactory factory = XPathFactory.newInstance();  
            XPath xpath = factory.newXPath();  
            // 选取所有class元素的name属性   
            // XPath语法介绍: http://w3school.com.cn/xpath/   
            XPathExpression expr = xpath.compile("//class/@name");  
            NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);  
            for (int i = 0; i < nodes.getLength(); i++) {  
                System.out.println("name = " + nodes.item(i).getNodeValue());  
                    }  
        } catch (XPathExpressionException e) {  
            e.printStackTrace();  
        } catch (ParserConfigurationException e) {  
            e.printStackTrace();  
        } catch (SAXException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}

 

六、【补充】

注意4种解析方法对TextNode(文本节点)的处理:

1、在使用DOM时,调用node.getChildNodes()获取该节点的子节点,文本节点也会被当作一个Node来返回,如:

<?xml version="1.0" encoding="UTF-8"?>  
<university name="pku">  
    <college name="c1">  
        <class name="class1">  
            <student name="stu1" sex=&#39;male&#39; age="21" />  
            <student name="stu2" sex=&#39;female&#39; age="20" />  
            <student name="stu3" sex=&#39;female&#39; age="20" />  
        </class>  
    </college>  
</university>
package test.xml;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.Arrays;  
import javax.xml.parsers.DocumentBuilder;  
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserConfigurationException;  
import org.w3c.dom.Document;  
import org.w3c.dom.Element;  
import org.w3c.dom.Node;  
import org.w3c.dom.NodeList;  
import org.xml.sax.SAXException;  
/** 
 * dom读写xml 
 * @author whwang 
 */  
public class TestDom2 {  
    public static void main(String[] args) {  
        read();  
    }  
    public static void read() {  
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
        try {  
            DocumentBuilder builder = dbf.newDocumentBuilder();  
            InputStream in = TestDom2.class.getClassLoader().getResourceAsStream("test.xml");  
            Document doc = builder.parse(in);  
            // root <university>   
            Element root = doc.getDocumentElement();  
            if (root == null) return;  
//          System.err.println(root.getAttribute("name"));   
            // all college node   
            NodeList collegeNodes = root.getChildNodes();  
            if (collegeNodes == null) return;  
            System.err.println("university子节点数:" + collegeNodes.getLength());  
            System.err.println("子节点如下:");  
            for(int i = 0; i < collegeNodes.getLength(); i++) {  
                Node college = collegeNodes.item(i);  
                if (college == null) continue;  
                if (college.getNodeType() == Node.ELEMENT_NODE) {  
                    System.err.println("\t元素节点:" + college.getNodeName());  
                } else if (college.getNodeType() == Node.TEXT_NODE) {  
                    System.err.println("\t文本节点:" + Arrays.toString(college.getTextContent().getBytes()));  
                }  
            }  
        } catch (ParserConfigurationException e) {  
            e.printStackTrace();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (SAXException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}


输出的结果是:

 
university子节点数:3  
子节点如下:  
    文本节点:[10, 9]  
    元素节点:college  
    文本节点:[10]

其中\n的ASCII码为10,\t的ASCII码为9。结果让人大吃一惊,university的子节点数不是1,也不是2,而是3,这3个子节点都是谁呢?为了看得更清楚点,把xml文档改为:

 
<?xml version="1.0" encoding="UTF-8"?>  
<university name="pku">11  
    <college name="c1">  
        <class name="class1">  
            <student name="stu1" sex=&#39;male&#39; age="21" />  
            <student name="stu2" sex=&#39;female&#39; age="20" />  
            <student name="stu3" sex=&#39;female&#39; age="20" />  
        </class>  
    </college>22  
</university>

还是上面的程序,输出结果为:

 
university子节点数:3  
子节点如下:  
    文本节点:[49, 49, 10, 9]  
    元素节点:college  
    文本节点:[50, 50, 10]


其中数字1的ASCII码为49,数字2的ASCII码为50。

2、使用SAX来解析同DOM,当你重写它的public void characters(char[] ch, int start, int length)方法时,你就能看到。

3、JDOM,调用node.getChildren()只返回子节点,不包括TextNode节点(不管该节点是否有Text信息)。如果要获取该节点的Text信息,可以调用node.getText()方法,该方法返回节点的Text信息,也包括\n\t等特殊字符

4、DOM4j同JDOM

The above is the detailed content of Detailed introduction to the sample code of four methods of parsing Xml. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

Building Feeds with XML: A Hands-On Guide to RSSBuilding Feeds with XML: A Hands-On Guide to RSSApr 14, 2025 am 12:17 AM

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

Creating RSS Documents: A Step-by-Step TutorialCreating RSS Documents: A Step-by-Step TutorialApr 13, 2025 am 12:10 AM

The steps to create an RSS document are as follows: 1. Write in XML format, with the root element, including the elements. 2. Add, etc. elements to describe channel information. 3. Add elements, each representing a content entry, including,,,,,,,,,,,. 4. Optionally add and elements to enrich the content. 5. Ensure the XML format is correct, use online tools to verify, optimize performance and keep content updated.

XML's Role in RSS: The Foundation of Syndicated ContentXML's Role in RSS: The Foundation of Syndicated ContentApr 12, 2025 am 12:17 AM

The core role of XML in RSS is to provide a standardized and flexible data format. 1. The structure and markup language characteristics of XML make it suitable for data exchange and storage. 2. RSS uses XML to create a standardized format to facilitate content sharing. 3. The application of XML in RSS includes elements that define feed content, such as title and release date. 4. Advantages include standardization and scalability, and challenges include document verbose and strict syntax requirements. 5. Best practices include validating XML validity, keeping it simple, using CDATA, and regularly updating.

From XML to Readable Content: Demystifying RSS FeedsFrom XML to Readable Content: Demystifying RSS FeedsApr 11, 2025 am 12:03 AM

RSSfeedsareXMLdocumentsusedforcontentaggregationanddistribution.Totransformthemintoreadablecontent:1)ParsetheXMLusinglibrarieslikefeedparserinPython.2)HandledifferentRSSversionsandpotentialparsingerrors.3)Transformthedataintouser-friendlyformatsliket

Is There an RSS Alternative Based on JSON?Is There an RSS Alternative Based on JSON?Apr 10, 2025 am 09:31 AM

JSONFeed is a JSON-based RSS alternative that has its advantages simplicity and ease of use. 1) JSONFeed uses JSON format, which is easy to generate and parse. 2) It supports dynamic generation and is suitable for modern web development. 3) Using JSONFeed can improve content management efficiency and user experience.

RSS Document Tools: Building, Validating, and Publishing FeedsRSS Document Tools: Building, Validating, and Publishing FeedsApr 09, 2025 am 12:10 AM

How to build, validate and publish RSSfeeds? 1. Build: Use Python scripts to generate RSSfeed, including title, link, description and release date. 2. Verification: Use FeedValidator.org or Python script to check whether RSSfeed complies with RSS2.0 standards. 3. Publish: Upload RSS files to the server, or use Flask to generate and publish RSSfeed dynamically. Through these steps, you can effectively manage and share content.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)