[Change] Comparison of the four operating mode 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 event-based model, which, when parsing XML documents can trigger a series of events, when discovered, when a given tag, which can activate a callback method, to tell the method to develop the label has been found. SAX on the memory requirements are usually lower, because it allows developers to decide which to deal with the tag. Especially when the developer need only deal with part of the document contained in the data, SAX is better scalability of this embodiment. 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 problems" (according to 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 that you fully understand the XML to do some work beyond the basic (or even understand some cases, the 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 input XML document (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 the API in the Collections class, but in many cases, it also provides some alternative ways to allow for better performance or a more direct encoding 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 the Java Developer's ease of 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.

Comparison of 2 ..

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 XML configuration files. 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. In the small document cases, is worth considering the use DOM and JDOM. While JDOM developers have shown they expect to focus on performance before the official release issue, 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 a number of other standards and XML-related basis, because it formally W3C Recommendation (based on non-standard Java model over), so certain types of projects may also need it (such as the use of the JavaScript 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).

3. Xml mode of operation of four basic use

xml file:

<? Xml version = "1.0" encoding = "utf-8"?>
<NO DATE="2005"> A1 </ NO>
<NO DATE="2004"> A2 </ NO>
</ Result>

1) DOM

import java.io. *;
import java.util .*;
import org.w3c.dom .*;
import javax.xml.parsers .*;

public class MyXMLReader (
public static void main (String arge []) (

long lasting = System.currentTimeMillis ();
try (
File f = new File ("data_10k.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ();
DocumentBuilder builder = factory.newDocumentBuilder ();
Document doc = builder.parse (f);
NodeList nl = doc.getElementsByTagName ("VALUE");
for (int i = 0; i <nl.getLength (); i + +) (
System.out.print ("license plate number:" + doc.getElementsByTagName ("NO"). Item (i). GetFirstChild (). GetNodeValue ());
System.out.println ("owner address:" + doc.getElementsByTagName ("ADDR"). Item (i). GetFirstChild (). GetNodeValue ());
) Catch (Exception e) (
e.printStackTrace ();

2) SAX

import org.xml.sax .*;
import org.xml.sax.helpers .*;
import javax.xml.parsers .*;

public class MyXMLReader extends DefaultHandler (

java.util.Stack tags = new java.util.Stack ();
public MyXMLReader () (
super ();

public static void main (String args []) (
long lasting = System.currentTimeMillis ();
try (
SAXParserFactory sf = SAXParserFactory.newInstance ();
SAXParser sp = sf.newSAXParser ();
MyXMLReader reader = new MyXMLReader ();
sp.parse (new InputSource ("data_10k.xml"), reader);
) Catch (Exception e) (
e.printStackTrace ();

System.out.println ("running time:" + (System.currentTimeMillis () - lasting) + "ms");)
public void characters (char ch [], int start, int length) throws SAXException (
String tag = (String) tags.peek ();
if (tag.equals ("NO")) (
System.out.print ("license plate number:" + new String (ch, start, length));
if (tag.equals ("ADDR")) (
System.out.println ("Address:" + new String (ch, start, length));

public void startElement (String uri, String localName, String qName, Attributes attrs) (
tags.push (qName);)


import java.io. *;
import java.util .*;
import org.jdom .*;
import org.jdom.input .*;

public class MyXMLReader (

public static void main (String arge []) (
long lasting = System.currentTimeMillis ();
try (
SAXBuilder builder = new SAXBuilder ();
Document doc = builder.build (new File ("data_10k.xml"));
Element foo = doc.getRootElement ();
List allChildren = foo.getChildren ();
for (int i = 0; i <allChildren.size (); i + +) (
System.out.print ("license plate number:" + ((Element) allChildren.get (i)). GetChild ("NO"). GetText ());
System.out.println ("owner address:" + ((Element) allChildren.get (i)). GetChild ("ADDR"). GetText ());
) Catch (Exception e) (
e.printStackTrace ();


4) DOM4J

import java.io. *;
import java.util .*;
import org.dom4j .*;
import org.dom4j.io .*;

public class MyXMLReader (

public static void main (String arge []) (
long lasting = System.currentTimeMillis ();
try (
File f = new File ("data_10k.xml");
SAXReader reader = new SAXReader ();
Document doc = reader.read (f);
Element root = doc.getRootElement ();
Element foo;
for (Iterator i = root.elementIterator ("VALUE"); i.hasNext ();) (
foo = (Element) i.next ();
System.out.print ("license plate number:" + foo.elementText ("NO"));
System.out.println ("owner address:" + foo.elementText ("ADDR"));
) Catch (Exception e) (
e.printStackTrace ();

分类:Web 时间:2010-03-11 人气:219
blog comments powered by Disqus


  • java in the comparison of four operating modes xml 2010-11-12

    java in the comparison of four operating modes xml 1. Introduction 1) DOM (JAXP Crimson parser) DOM is platform and language independent way, said an official W3C standard XML documents. DOM is a hierarchical organization of nodes or a collection of

  • [Change] Comparison of four kinds of operation mode XML 2008-12-09

    1. Introduction 1) DOM (JAXP Crimson parser) DOM is platform-and language-independent manner in the official W3C standard XML documents. DOM is a hierarchical organization of nodes or a collection of pieces of information. This hierarchical structure

  • [Change] how to improve the operating speed of the cycle of JavaScript articles (1) 2010-08-02

    According to Nicholas to say, there are four kinds of code will slow down the script to run, and eventually the script out of control. Are too frequent synchronization loop, a huge body of the function, inappropriate and unreasonable DOM recursive ca

  • [Change] how to improve the operating speed of the JavaScript function articles (2) 2010-08-02

    This is Nicholas discuss efforts to prevent loss of control of the second script, mainly discussed how to reconstruct nested loops, recursion, and those who perform many functions within the same sub-operation of the function. The basic idea, and on

  • [Change] Comparison of five open source license (BSD, Apache, GPL, LGPL, MIT) 2010-08-27

    When Adobe, Microsoft, Sun and a series of giant began to show on the "open source" of all ages, the "open source" the time soon come! Originally from: sinoprise.com / read.php? Tid-662-page-e-fpage-1.html (Unfortunately, this link has

  • [Change] how to improve the operating speed of the recursive JavaScript articles (3) 2010-08-02

    Affect the performance of another killer JavaScript is recursive, is mentioned in the previous section can be optimized using memoization techniques to calculate the recursive function, but memoization is not a panacea, not all recursive functions ca

  • [Change] JNDI article 2010-03-29

    Print edition 1] ------------ JNDI is the Java Naming and Directory Interface (Java Naming and Directory Interface), in the J2EE specification is one of the important specifications, many experts believe that without a thorough understanding of the s

  • Flex-based Flex-config.xml command line compiler 2010-05-31

    Why should recommend ways to use flex-config.xml it, flex-config.xml flex compiler and other control covers all the options for the command line compiler, mxmlc compiler options for a complex command-line input is simply a nightmare and use of flex-c

  • dom4j students complete CRUD (change) 2010-06-15

    dom4j is an open source parsing XML documents XML framework, this paper to introduce the students to use it to complete the object of the CRUD operations. dom4j API XML document contains an analysis tool, compared with the W3C DOM API, use the dom4j

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



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

CodeWeblog.com 版权所有 闽ICP备15018612号

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