(Reprinted) Java parsing XML in the four methods

[Original article name: Java parsing XML documents - dom parsing xml]

1. Introduction

1) DOM (JAXP Crimson parser)

DOM is platform-and language-independent manner, said the official W3C standard XML document. DOM is a hierarchical organization of nodes or pieces of information collection. This hierarchy allows the developer to find specific information in the tree. Analysis of the structure often requires loading and structural hierarchy of the entire document before doing any work. Because it is based on the information level, which is considered DOM tree-based or object-based. DOM and the broad tree-based processing has several advantages. First, because the tree in memory is durable, so you can modify it so that applications can be made on data and structural changes. It may at any time up and down navigation in the tree, rather than as a one-time processing SAX. DOM also much easier to use.

2) SAX

The advantages of SAX processing is very similar to the advantages of streaming media. Analysis can begin immediately rather than waiting for all data are processed. And, because the application reads the data only at the check data, so no data is stored in memory. This large document is a huge advantage. In fact, the application even without parsing the entire document; it can stop when a condition is met resolve. In general, SAX is also an alternative to DOM than its much faster.

Select or Select DOM SAX? The need to write your own code to handle XML document, developers, choose DOM or SAX parsing model is a very important design decisions. DOM tree structure used to establish access to XML documents, while the SAX event model used.

DOM parser, the XML document into a tree that contains the content, and can traverse the tree. DOM parsing model with the advantages of easy programming, developers need only call the achievements of the command, and then using the tree navigation APIs to access the node to complete the required tasks. Can easily add and modify elements of the tree. However, when the use of DOM parser to handle the entire XML document, so performance and memory requirements is relatively high, particularly at a time when much of the XML file. Because of its ability to traverse, DOM parser, XML documents commonly used in frequently changing needs of the services.

SAX parser using the event-based model, which, when parsing XML documents can trigger a series of events, it became apparent that when a given tag, which can activate a callback method, to tell the method The labeling has been found. SAX on would be more memory requirements are usually low, because it allows developers themselves Jue Dingsuo staff to deal with the tag. Tebie is Dang development staff only deal Wendang in the Bao Han Xu Yao's Bufenshuoju O'clock, SAX Zhezhong Kuozhannengli is better to expression. But with the SAX parser when the coding would be more difficult, and it is very difficult to access the same document in multiple different data.

3) JDOM http://www.jdom.org

JDOM is intended to be Java-specific document model, which simplifies the interaction with XML, and faster than using DOM. Because it is the first Java -specific models, JDOM has been vigorously promoted and promoted. Is considering "Java Specification Request JSR-102" will it eventually be used as "Java standard extension." From early 2000 had started JDOM development.

JDOM and DOM are mainly two different. First of all, JDOM uses concrete classes rather than just using the interface. This is in some ways simplifies the API, but also limits flexibility. Second, API extensive use of Collections class, simplifying those already familiar with these kind of Java developers use.

JDOM document to declare that their aim is to "use 20% (or less) 80% of the energy solution (or more) Java / XML problem" (based on the learning curve is assumed to be 20%). JDOM for the majority of Java / XML applications, of course, useful, and most developers find that API is much easier to understand than the DOM. JDOM also includes a wide range of program behavior in check to prevent users from doing anything in XML in insignificant things. However, it still requires you to fully understand the XML to do some work beyond the basic (or even understand some cases of error). This may be better than learning the DOM or JDOM interfaces are more meaningful work.

JDOM itself does not contain a parser. It usually uses SAX2 parser to parse and validate XML document input (although it can be said before the DOM structures as input). It contains the JDOM said converter to output a SAX2 event streams, DOM model or XML text document. JDOM is a variant of the Apache License released under open source.

4) Dom4J http://dom4j.sourceforge.net

Although DOM4J represents the results of completely independent development, but initially, it is an intelligence branch of JDOM. It incorporates many of the basic XML document that go beyond the features, including integrated support for XPath, XML Schema support, and for large documents or stream of event-based document processing. It also provides the option of constructing the document indicated that it passed DOM4J API and standard DOM interfaces with parallel access. From the second half of 2000, it has been under development.

To support all these features, DOM4J use interfaces and abstract base class methods. DOM4J extensive use of API in the Collections The class , but in many cases, the it provides A few alternatives Yi allowing for better performance or more of the Bian Ma Zhijie method. Direct benefit, although DOM4J more complex API to pay the price, but it offers much greater flexibility than JDOM.

Adding flexibility, XPath integration and processing of large documents objectives, DOM4J goals and JDOM is the same: for Java developers to use and intuitive operation. It also aims to be more complete than JDOM solution to achieve, in essence, to deal with all Java / XML, the objective. Upon completion of the target, less stressed than JDOM prevent incorrect application behavior.

DOM4J is a very, very good Java XML API, high-performance, powerful and extremely easy to use features, while it is an open source software. Now you can see more and more Java software are in use DOM4J to read and write XML, is particularly worth mentioning is that even the Sun's JAXM also use DOM4J.

2, compare

1) DOM4J best performance, even the Sun's JAXM also use DOM4J. At present, many open source projects are widely used DOM4J, such as the famous Hibernate also used DOM4J to read the XML configuration file. If you do not consider the portability, then use DOM4J.

2) JDOM and DOM in the performance of the test performed poorly in tests of memory 10M document. Under conditions of small documents is also worth to consider using DOM and JDOM. Although JDOM developers have instructions they expect the official release version of the former focus on performance issues, but from a performance point of view, it does not recommend it. In addition, DOM is still a very good option. DOM implementation used in a variety of programming languages. It is also relevant to many other XML-based standards, because it formally W3C recommendation (non-standard Java-based model of relative), so in certain types of projects may also need it (such as JavaScript using DOM).

3) SAX performance is better, that depends on its specific analytical method - event-driven. Detection of a SAX XML stream coming, but not loaded into memory, (of course, when the XML stream is read, some of the documents being hidden in the memory).

-------------------------------------------------- -------------------------------------------------- -----------------------------------

[Original article Name: Xiang Jie four methods of Java XML parsing]

XML is now Yijingchengwei a common data exchange format, it's platform independence, language independence, system independence, Ji data Ji Cheng and Jiaohudailai a great convenience. The syntax for the XML itself, knowledge and technical details, to read the relevant technical literature, which involves the contents of DOM (Document Object Model), DTD (Document Type Definition), SAX (Simple API for XML), XSD (Xml Schema Definition ), XSLT (Extensible Stylesheet Language Transformations), specifically refer to the document http://www.w3.org w3c website for more information.

XML parsing in a different language in the same way, only to achieve the syntax is different. There are two basic analytic approach, one called SAX, the other is called DOM. SAX is event-based stream parsing, DOM is based on the XML document tree structure analysis. Suppose we have the content and structure of XML is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee>
<name>ddviplinux</name>
<sex>m</sex>
<age>30</age>
</employee>
</employees>

This article uses the JAVA language to implement DOM and SAX in the XML document generation and analysis.
First define an operation interface XmlDocument XML document XML document that defines the establishment and analysis interface.

package com.alisoft.facepay.framework.bean;
/**
*
* @author hongliang.dinghl
*  Defines the XML document of the interface
*/
public interface XmlDocument {
/**
*  Create an XML document
* @param fileName  Full path name of the file
*/
public void createXml(String fileName);
/**
*  Parse the XML document
* @param fileName  Full path name of the file
*/
public void parserXml(String fileName);
}

1.DOM generating and parsing XML documents

For the analytical version of the XML document has defined a set of interfaces. Parser to read the entire document, and then build a memory-resident tree structure, then the code can use DOM interfaces to manipulate the tree structure. Advantages: the entire document tree in memory, easy to operate; support delete, edit, rearrange and other functions; Disadvantages: the entire document will be transferred to memory (including no use of the node), a waste of time and space; Use: Once the analysis many times the document needs to access the data; sufficient hardware resources (memory, CPU).

package com.alisoft.facepay.framework.bean;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
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.xml.sax.SAXException;

/**
 *
 * @author hongliang.dinghl DOM Generate and parse an XML document
 */
public class DomDemo implements XmlDocument {
        private Document document;
        private String fileName;

        public void init() {
                try {
                        DocumentBuilderFactory factory = DocumentBuilderFactory
                                        .newInstance();
                        DocumentBuilder builder = factory.newDocumentBuilder();
                        this.document = builder.newDocument();
                } catch (ParserConfigurationException e) {
                        System.out.println(e.getMessage());
                }
        }

        public void createXml(String fileName) {
                Element root = this.document.createElement("employees");
                this.document.appendChild(root);
                Element employee = this.document.createElement("employee");
                Element name = this.document.createElement("name");
                name.appendChild(this.document.createTextNode(" Ding inmates  "));
                employee.appendChild(name);
                Element sex = this.document.createElement("sex");
                sex.appendChild(this.document.createTextNode("m"));
                employee.appendChild(sex);
                Element age = this.document.createElement("age");
                age.appendChild(this.document.createTextNode("30"));
                employee.appendChild(age);
                root.appendChild(employee);
                TransformerFactory tf = TransformerFactory.newInstance();
                try {
                        Transformer transformer = tf.newTransformer();
                        DOMSource source = new DOMSource(document);
                        transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
                        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                        PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
                        StreamResult result = new StreamResult(pw);
                        transformer.transform(source, result);
                        System.out.println(" To generate the XML file successfully  !");
                } catch (TransformerConfigurationException e) {
                        System.out.println(e.getMessage());
                } catch (IllegalArgumentException e) {
                        System.out.println(e.getMessage());
                } catch (FileNotFoundException e) {
                        System.out.println(e.getMessage());
                } catch (TransformerException e) {
                        System.out.println(e.getMessage());
                }
        }

        public void parserXml(String fileName) {
                try {
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db = dbf.newDocumentBuilder();
                        Document document = db.parse(fileName);
                        NodeList employees = document.getChildNodes();
                        for (int i = 0; i < employees.getLength(); i++) {
                                Node employee = employees.item(i);
                                NodeList employeeInfo = employee.getChildNodes();
                                for (int j = 0; j < employeeInfo.getLength(); j++) {
                                        Node node = employeeInfo.item(j);
                                        NodeList employeeMeta = node.getChildNodes();
                                        for (int k = 0; k < employeeMeta.getLength(); k++) {
                                                System.out.println(employeeMeta.item(k).getNodeName()
                                                                + ":" + employeeMeta.item(k).getTextContent());
                                        }
                                }
                        }
                        System.out.println(" Parsing  ");
                } catch (FileNotFoundException e) {
                        System.out.println(e.getMessage());
                } catch (ParserConfigurationException e) {
                        System.out.println(e.getMessage());
                } catch (SAXException e) {
                        System.out.println(e.getMessage());
                } catch (IOException e) {
                        System.out.println(e.getMessage());
                }
        }
}

2.SAX generating and parsing XML documents

DOM to solve the problem, there SAX. SAX, event-driven. When the parser found element start element end, text, document the beginning or end, etc., send event, programmers write code to respond to these events, save the data. Advantages: not transferred to the entire document in advance, taking up less resources; SAX parser source code than the DOM parser is small, suitable for Applet, download. Disadvantages: not durable; event after, if not save the data, then data is lost; no state of; from the event can only get the text, but I do not know which elements of the text is; Use: Applet; only XML document of a small amount of content, very little back to visit; machine less memory;

package com.alisoft.facepay.framework.bean;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author hongliang.dinghl SAX Document analysis
 */
public class SaxDemo implements XmlDocument {

        public void createXml(String fileName) {
                System.out.println("<<" + filename + ">>");
        }

        public void parserXml(String fileName) {
                SAXParserFactory saxfac = SAXParserFactory.newInstance();

                try {

                        SAXParser saxparser = saxfac.newSAXParser();

                        InputStream is = new FileInputStream(fileName);

                        saxparser.parse(is, new MySAXHandler());

                } catch (ParserConfigurationException e) {

                        e.printStackTrace();

                } catch (SAXException e) {

                        e.printStackTrace();

                } catch (FileNotFoundException e) {

                        e.printStackTrace();

                } catch (IOException e) {

                        e.printStackTrace();

                }

        }

}

class MySAXHandler extends DefaultHandler {

        boolean hasAttribute = false;

        Attributes attributes = null;

        public void startDocument() throws SAXException {

                System.out.println(" Start printing the document  ");

        }

        public void endDocument() throws SAXException {

                System.out.println(" At the end of the document is printed  ");

        }

        public void startElement(String uri, String localName, String qName,

        Attributes attributes) throws SAXException {

                if (qName.equals("employees")) {

                        return;

                }

                if (qName.equals("employee")) {

                        System.out.println(qName);

                }

                if (attributes.getLength() > 0) {

                        this.attributes = attributes;

                        this.hasAttribute = true;

                }

        }

        public void endElement(String uri, String localName, String qName)

        throws SAXException {

                if (hasAttribute && (attributes != null)) {

                        for (int i = 0; i < attributes.getLength(); i++) {

                                System.out.println(attributes.getQName(0)
                                                + attributes.getValue(0));

                        }

                }

        }

        public void characters(char[] ch, int start, int length)

        throws SAXException {

                System.out.println(new String(ch, start, length));

        }

}
package com.alisoft.facepay.framework.bean;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author hongliang.dinghl SAX Document analysis
 */
public class SaxDemo implements XmlDocument {
        public void createXml(String fileName) {
                System.out.println("<<" + filename + ">>");
        }

        public void parserXml(String fileName) {
                SAXParserFactory saxfac = SAXParserFactory.newInstance();
                try {
                        SAXParser saxparser = saxfac.newSAXParser();
                        InputStream is = new FileInputStream(fileName);
                        saxparser.parse(is, new MySAXHandler());
                } catch (ParserConfigurationException e) {
                        e.printStackTrace();
                } catch (SAXException e) {
                        e.printStackTrace();
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }
        }
}

class MySAXHandler extends DefaultHandler {
        boolean hasAttribute = false;
        Attributes attributes = null;

        public void startDocument() throws SAXException {
                System.out.println(" Start printing the document  ");
        }

        public void endDocument() throws SAXException {
                System.out.println(" At the end of the document is printed  ");
        }

        public void startElement(String uri, String localName, String qName,
                        Attributes attributes) throws SAXException {
                if (qName.equals("employees")) {
                        return;
                }
                if (qName.equals("employee")) {
                        System.out.println(qName);
                }
                if (attributes.getLength() > 0) {
                        this.attributes = attributes;
                        this.hasAttribute = true;
                }
        }

        public void endElement(String uri, String localName, String qName)
                        throws SAXException {
                if (hasAttribute && (attributes != null)) {
                        for (int i = 0; i < attributes.getLength(); i++) {
                                System.out.println(attributes.getQName(0)
                                                + attributes.getValue(0));
                        }
                }
        }

        public void characters(char[] ch, int start, int length)
                        throws SAXException {
                System.out.println(new String(ch, start, length));
        }
}

3.DOM4J generating and parsing XML documents

DOM4J is a very, very good Java XML API, high-performance, powerful and extremely easy to use features, while it is an open source software. Now you can see more and more Java software are in use DOM4J to read and write XML, is particularly worth mentioning is that even the Sun's JAXM also use DOM4J.

package com.alisoft.facepay.framework.bean;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author hongliang.dinghl Dom4j  Generate XML documentation and analysis  XML Document
 */
public class Dom4jDemo implements XmlDocument {

        public void createXml(String fileName) {
                Document document = DocumentHelper.createDocument();
                Element employees = document.addElement("employees");
                Element employee = employees.addElement("employee");
                Element name = employee.addElement("name");
                name.setText("ddvip");
                Element sex = employee.addElement("sex");
                sex.setText("m");
                Element age = employee.addElement("age");
                age.setText("29");
                try {
                        Writer fileWriter = new FileWriter(fileName);
                        XMLWriter xmlWriter = new XMLWriter(fileWriter);
                        xmlWriter.write(document);
                        xmlWriter.close();
                } catch (IOException e) {

                        System.out.println(e.getMessage());
                }

        }

        public void parserXml(String fileName) {
                File inputXml = new File(fileName);
                SAXReader saxReader = new SAXReader();
                try {
                        Document document = saxReader.read(inputXml);
                        Element employees = document.getRootElement();
                        for (Iterator i = employees.elementIterator(); i.hasNext();) {
                                Element employee = (Element) i.next();
                                for (Iterator j = employee.elementIterator(); j.hasNext();) {
                                        Element node = (Element) j.next();
                                        System.out.println(node.getName() + ":" + node.getText());
                                }

                        }
                } catch (DocumentException e) {
                        System.out.println(e.getMessage());
                }
                System.out.println("dom4j parserXml");
        }
}

4.JDOM generating and parsing XML

To reduce the DOM, SAX encoding volume, there JDOM; advantages :20-80 principle, greatly reduce the amount of code. Use: To achieve a simple function, such as analysis, creating, etc., but at the bottom, JDOM, or use the SAX (most common), DOM, Xanan document.

package com.alisoft.facepay.framework.bean;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author hongliang.dinghl JDOM  Generate and parse an XML document
 *
 */
public class JDomDemo implements XmlDocument {

        public void createXml(String fileName) {
                Document document;
                Element root;
                root = new Element("employees");
                document = new Document(root);
                Element employee = new Element("employee");
                root.addContent(employee);
                Element name = new Element("name");
                name.setText("ddvip");
                employee.addContent(name);
                Element sex = new Element("sex");
                sex.setText("m");
                employee.addContent(sex);
                Element age = new Element("age");
                age.setText("23");
                employee.addContent(age);
                XMLOutputter XMLOut = new XMLOutputter();
                try {
                        XMLOut.output(document, new FileOutputStream(fileName));
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }

        public void parserXml(String fileName) {
                SAXBuilder builder = new SAXBuilder(false);
                try {
                        Document document = builder.build(fileName);
                        Element employees = document.getRootElement();
                        List employeeList = employees.getChildren("employee");
                        for (int i = 0; i < employeeList.size(); i++) {
                                Element employee = (Element) employeeList.get(i);
                                List employeeInfo = employee.getChildren();
                                for (int j = 0; j < employeeInfo.size(); j++) {
                                        System.out.println(((Element) employeeInfo.get(j))
                                                        .getName()
                                                        + ":" + ((Element) employeeInfo.get(j)).getValue());
                                }
                        }
                } catch (JDOMException e) {
                        e.printStackTrace();
                } catch (IOException e) {
                        e.printStackTrace();
                }

        }

}
分类:Java 时间:2010-04-16 人气:321
分享到:
blog comments powered by Disqus

相关文章

  • Java programming language commonly used in the method of updating XML documents 2010-09-28

    Quoted from http://www.ibm.com/developerworks/cn/java/l-javaxml/ This paper briefly discusses the Java programming language update XML documents in the four commonly used methods, and analysis of the merits of the four methods. Secondly, the article also

  • XML documents using Libxml2 operation 2010-02-25

    1, Libxml2 Description: Libxml2 is an xml parser c-language version, was originally a tool for the Gnome project development, based on the MIT License is a free open source software. It addition to supporting the c-language version, it also supports c + +

  • Java parsing XML documents four methods 2010-05-07

    1) DOM (JAXP Crimson parser) DOM is platform-and language-independent manner, said the official W3C standard XML document. DOM is a hierarchical organization of nodes or pieces of information collection. This hierarchy allows the developer to find sp ...

  • Database holds a large number of XML documents (that exist) 2010-10-19

    Relational database to store large XML documents can cause many problems. Solve these problems is the first step in a deep understanding of the parameters of an XML document. Then the correct design appropriate solutions. Solutions may include: a ser

  • ROR at acts_as_tree increase xtree generate dynamic tree structure under the collection 2009-03-14

    Because of the recent study used this thing, so recorded for future reference Main function is to use acts_as_tree and XTree to achieve dynamic tree structure 1, about XTREE Download: You can search online, or directly to the official website downloa ...

  • DOM4J on XML documents to read and write operations such as additions and deletions to change 2010-03-16

    XML documents with DOM4J on CRUD operations such as reading and writing is my own exercises, and notes there is no order and only later see use. Main method in the constructor to do the simple instructions, involved XML, XSD, DTD document is no longer wri

  • (Transfer) the use of PL / SQL DOM API parsing of XML documents 2010-04-17

    This program demonstrates the use of PL / SQL DOM API for parsing XML documents, - Use the SYS.XMLDOM and SYS.XMLPARSER package - Program Analysis of the input XML file, print the document for all elements and element attributes connect scott / tiger ...

  • Android to create XML documents 2010-08-10

    Now, we just look at a concrete example to illustrate how to use the XmlSerializer. private String writeXml () ( XmlSerializer serializer = Xml.newSerializer (); StringWriter writer = new StringWriter (); try ( serializer.setOutput (writer); / / <? Xml

  • Using dom4j to create, edit XML documents and format the output and the Chinese to solve the problem 2010-08-22

    package com.holen.dom4j; import java.io.File; import java.io.FileWriter; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.do

  • Parsing XML documents using DOM 2010-12-12

    DOM parsing XML documents must have the three-step operation: 1, be an instance of DOM parser factory class; 2, by DOM parser object; 3, get the Document object represents the entire document. The following code shows: DocumentBuilderFactory dbf = Do

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.704 (s). 12 q(s)