(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();
                }

        }

}
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of (Reprinted) Java parsing XML in the four methods

  • Study Application hibernate (1)

    Basic configurations: Environment: myeclipse Version: 5.5.1 tomcat version: 5.5 1, myEclipse to adjust to the MyEclipse Database Explorer mode; a new database connection, the need for database-driven 2, myEclipse to adjust to the MyEclipse Hibernate ...

  • hibernate brief introduction

    1: hibernate, what are? 1: hibernate is a JDBC package is responsible for object persistence, in the middle layer, in the application and database has played a role as a bridge between a mapping tool. Second: Why is the use of hibernate? 1: the use of hib

  • Shoes - Construction Ruby client gadget

    Shoes Ruby are cross-platform GUI applications, recently released a new version, it is a very interesting Ruby GUI client, the following are some characteristics of Introduction: 1. Cross-platform support for windows / linux / mac os 2. Very small, t ...

  • Universal Data Access Control System Design

    Figure II: general data rights management system database design Comparison of two maps, we can see that the main changes between them are: 1, increase system resources, information and operation type information, the system resources for the tree st ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • The EJB3 Persistence

    EJB3 persistence with Hibernate is very similar to the mechanism: Environment: Server: JBOSS5.0 Database: MySQL5.0 1. Set up a data source First of all, in jboss-5.0.0.GA \ server \ default \ deploy, the establishment of a database used to connect the dat

  • Based on JDBC, JPA Annotation achieve simple CRUD Generic Dao

    The origin of ideas are pretty long history of reasons: [Use iBATIS history] The use of iBATIS has been a long time, the system is to use the CRUD template tool to generate the code, although there are tools to generate, but looked at a lot of CRUD the Sq

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries