As mentioned in the introduction Sun now provides these tools for XML Processing in Java:

  1. StAX Reader / Writer
  2. SAX Parser
  3. DOM Parser
  4. XPath Evaluator
  5. XSL Processor
  6. JAXB

In the following sections I will talk a bit about what these tools are, and what their purposes are.

StAX

The Java StAX API is a streaming API for reading and writing XML streams. As such it resembles the older SAX API, except that the SAX API can only be used to read XML, not write it.

The main difference between the SAX and StAX API is that when using SAX you provide a handler to the SAX parser. This handler then has methods called on it, corresponding to the entitites (elements, comments, text etc.) found in the XML document. It is the SAX parser that controls the iteration through the XML document. With the StAX parser you control the iteration through the XML document. You call the next() method (or corresponding method) when you feel you are ready to process the next element, text node etc. From the next() method you obtain an object that can tell you what entity you have met in the XML document.

Controlling the iteration through the XML document like this can be an advantage. The iteration can be kept within the scope of one method (perhaps calling submethods). This means that you can use / access the same local variables when processing a piece of text, as when processing an element. With SAX, these two entities are handled by two different methods in your handler object. Thus, if you need to access shared variables from these different methods, these variables has to be member variables in the handler object. Not a huge difference, but local variables may still be preferable in many situations.

The StAX API interfaces comes with Java 6, but there is yet no implementation. A standard implementation can be found at stax.codehaus.org.

SAX Parser

The SAX parser is the first API for processing XML entity by entity (element, text, comments etc.) as they are met during traversal of the XML document. When you use a SAX parser you pass a handler object to the SAX parser. This handler object has a method for every "event" you want to handle, during the traversal of the XML document. Examples of events are startDocumentstartElementcharacters etc.

The SAX parser is mostly suited for processing XML documents where each element can be processed individually. Documents where you need access to earlier or later parts of the document, to process a given element, are not as easy to handle. Well, if you need access to earlier parts, you can store that earlier part when it occurs in a member variable in the handler object. But if you need access to later elements, this is kind of difficult. Unless you process the earlier elemements when the later elements needed are encountered. Yet, if you need to jump around the XML document when processing it, it might be easier to use a DOM parser.

In most cases where you find a SAX parser useful you may be better off using a StAX reader instead.

The SAX interfaces and implementation comes with Java (at least from Java 5).

DOM Parser

The DOM (Document Object Model) parser parses an XML document into an object graph. The whole document is converted into one big objects. Once created you can traverse the object graph at will. You can walk up and down in the graph as you please. This object graph takes up a lot of memory, so this should only be used in situations where no other options are suitable.

The DOM interfaces and implementation comes with Java (at least from Java 5).

XPath Evaluator

Java comes with a built-in XPath evaluator. You set up an XPath expression, and have the evaluator evalute the expression on a DOM object. The evaluator then returns the elements matching the XPath expression. XPaths can be a handy way of finding the nodes you need to process, rather than navigate down to them yourself.

XSL Processor

Java comes with a built-in XSL Processor. An XSL Processor transforms an input XML document to an output document (not necessarily XML), according to an XSL stylesheet. An example appliance of a stylesheet would be to transform an XML document containing data, to an HTML document with that data presented in a more humanly readable format, for instance in tables, lists etc.

JAXB

JAXB is an API that resembles the DOM API. JAXB lets you generate classes from an XSL Schema, matching the XML document defined in that schema. JAXB then lets you read an XML document conforming to that schema, into the an object structure built from the generated objects. You can also serialize that object structure back to disk or network.

The generated JAXB classes looks more like regular domain objects. They have getters and setters with names matching the element names. The DOM API just has methods like addElement() etc. where the concrete element name is a parameter, and you need to know what elements can be added as children at any given element in the DOM structure. The JAXB generated clasess thus help you more, by reflecting the allowed structure in class and method names.

Java & XML Tool Overview的更多相关文章

  1. Java & XML Tutorial

    Java comes with a set of tools to process XML. These Java XML tools are: SAX Parser StAX Parser DOM ...

  2. Java XML解析工具 dom4j介绍及使用实例

    Java XML解析工具 dom4j介绍及使用实例 dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory ...

  3. 【Java】Java XML 技术专题

    XML 基础教程 XML 和 Java 技术 Java XML文档模型 JAXP(Java API for XML Parsing) StAX(Streaming API for XML) XJ(XM ...

  4. Java XML解析器

    使用Apache Xerces解析XML文档 一.技术概述 在用Java解析XML时候,一般都使用现成XML解析器来完成,自己编码解析是一件很棘手的问题,对程序员要求很高,一般也没有专业厂商或者开源组 ...

  5. Java XML Dom解析工具

    Java XML Dom解析工具 缩进等 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); ...

  6. Java XML DOM解析范例源码

    下边内容内容是关于Java XML DOM解析范例的内容.import java.io.InputStream; import java.util.ArrayList; import java.uti ...

  7. XML概念定义以及如何定义xml文件编写约束条件java解析xml DTD XML Schema JAXP java xml解析 dom4j 解析 xpath dom sax

    本文主要涉及:xml概念描述,xml的约束文件,dtd,xsd文件的定义使用,如何在xml中引用xsd文件,如何使用java解析xml,解析xml方式dom sax,dom4j解析xml文件 XML来 ...

  8. jvisualvm All-in-One Java Troubleshooting Tool

    java 监控.故障.性能可视化分析 VisualVM: Download https://visualvm.github.io/download.html All-in-One Java Troub ...

  9. Java&Xml教程(九)Java中通过XSD校验XML合法性

    Java XML校验API能够通过XSD(XML Schema Definition)校验XML文件内容的合法性.在下面的案例中使用javax.xml.validation.Validator 类通过 ...

随机推荐

  1. java 读取文件中文乱码问题

    很少写java io的代码,今天整了一个发现 本地调试好好的,放到jmeter里就打印乱码.一番折腾,终于搞定~直接上代码: List<Order> orderList = new Arr ...

  2. VMware 11完全安装Mac OS X 10.10

    本文已迁移到我的个人网站 http://www.wshunli.com 文章地址: http://www.wshunli.com/2016/03/17/VMware-12安装Mac-OS-X-10-1 ...

  3. 【HDOJ】4341 Gold miner

    分组01背包.在一条直线上的点归为一组. /* 4341 */ #include <iostream> #include <sstream> #include <stri ...

  4. Pyhon中的除法

    Python中分为3种除法:传统除法.精确除法.地板除. 传统除法: 如果是整数除法则执行地板除,如果是浮点数除法则执行精确除法. >>>1/2 0 >>>1.0/ ...

  5. Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)

    题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...

  6. VBSCRIPT事件绑定(隐式)

    很多新版的浏览器都开始不支持VBSCRIPT 所以系统开始不断地有script错误,开始比较多地接触VBSCRIPT vbscript 和javascript 事件绑定的类似方法为 vbscript: ...

  7. jquery 分页控件(一)

    以前一直都是用别人的分页控件,虽然用得很爽,但总觉的还是自己写个小插件比较好,这个插件效果.代码等都有参照别人完成的控件.即便功能并不是那么完善,扩展性也不好,bug或许还很多.个人觉得,适合自己用就 ...

  8. JS实用代码收集

    1.JSON对象转换为字符串 //JSON对象转字符串 function jsonParseString(o) { try { if (o == undefined) { return "& ...

  9. win8.1 64 安装用友T3+sql2005-64步骤

    1. 环境:win8.1 64 专业版  4G内存  .net framwork 3.5 2.初始过程及所需软件 安装sql2008数据库,安装完T3发现并不支持此数据库,运行T3老是出现连接数据时的 ...

  10. Java 程序员在写 SQL 时常犯的 10 个错误

    Java程序员编程时需要混合面向对象思维和一般命令式编程的方法,能否完美的将两者结合起来完全得依靠编程人员的水准: 技能(任何人都能容易学会命令式编程) 模式(有些人用“模式-模式”,举个例子,模式可 ...