Java & XML Tool Overview

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.

时间: 2024-12-22 10:41:34

Java & XML Tool Overview的相关文章

Java & XML Tutorial

Java comes with a set of tools to process XML. These Java XML tools are: SAX Parser StAX Parser DOM Parser XPath Evaluator XSL Processor JAXB These are the Java XML processing tools this tutorial is focused on. The text Java & XML Tool Overview will

JAVA XML

                                             JAVA     XML DOM 优缺点:实现 W3C 标准,有多种编程语言支持这种解析方式,并且这种方法本身操作上简单快捷,十分易于初学者掌握.其处理方式是将 XML 整个作为类似树结构的方式读入内存中以便操作及解析,因此支持应用程序对 XML 数据的内容和结构进行修改,但是同时由于其需要在处理开始时将整个 XML 文件读入到内存中去进行分析,因此其在解析大数据量的 XML 文件时会遇到类似于内存泄露以及

第18完结篇-JAVA XML

第18完结篇-JAVA XML 每篇一句 :我们不缺方法,缺的是一往无前的决心和魄力 初学心得: 我们应该从中吸取教训,而不是累积伤痛 (笔者:JEEP/711)[JAVA笔记 | 时间:2017-05-21| JAVA XML ] 1.什么是XML Extensible Markup Language:翻译过来为可扩展标记语言 Xml技术是w3c组织发布的,目前推荐遵循的是W3C组织于2000发布的XML1.0规范 在XML语言中,它允许用户自定义标签 一个标签用于描述一段数据:一个标签可分为

Java XML - JDOM2

Java XML - JDOM2 - Introduction (2014/9/28 22:10:49) What is JDOM JDOM is an in-memory XML model that can be used to read, write, create and modify XML Documents. JDOM is similar to DOM in that they both provide an in-memory XML document model, but w

java XML解析

package com.kpsh.myself; import java.io.File;import java.io.FileInputStream;import java.util.List; import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory; import org.dom4j.Document;import org.dom4j.DocumentHelper;imp

Java XML解析技术

XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便.XML在不同的语言里解析方式都是一样的,只不过实现的语法不同而已.基本的解析方式有两种,一种叫SAX,另一种叫DOM. DOM的全称是Document Object Model,也即文档对象模型.在应用程序中,基于DOM的XML分析器将一个XML文档转换成一个对象模型的集合(通常称DOM树),应用程序正是通过对这个对象模型的操作,来实现对XML文档数据的操作.通过DOM接口,应用程

【Java】Java XML 技术专题

XML 基础教程 XML 和 Java 技术 Java XML文档模型 JAXP(Java API for XML Parsing) StAX(Streaming API for XML) XJ(XML Enhancements for Java) XML 验证 XPath XQuery XSL 转换处理器 XStream 数据绑定 本专题汇总了大量面向 Java 开发人员的 XML 技术文章和教程,内容涉及 XML 基础.Java XML 的文档模型.编程 API 与数据绑定框架以及 Java

java xml处理技术一(解析xml和生存xml的技术)

java xml处理技术一 XML 技术是随着 Java 的发展而发展起来的.在 XML 出现之前对于简单的数据格式通常是存储在 ini 配置文件等文本文件中,复杂的格式则采用自定义的文件格式,因此对于每种文件格式都要有专门的解析程序. XML 出现以后解决了这个问题,程序面对的是有固定格式的 XML 文件,只要通过标准 API 就可以进行 XML 文件的处理. XML 文件在案例系统中应用是很广泛的,比如 ClientConfig.xml . ServerConfig.xml 文件就是使用 X

Java xml 解析

1. XML框架结构 Java SE 6 平台提供的 XML 处理主要包括两个功能:XML 处理(JAXP,Java Architecture XML Processing)和 XML 绑定(JAXB,Java Architecture XML Binding). JAXP 包括 SAX 框架 —— 遍历元素,做出处理:DOM 框架 —— 构造 XML 文件的树形表示:StAX 框架 —— 拖拽方式的解析:XSLT 框架 —— 将 XML 数据转换成其他格式.JAXB 则是负责将 XML 文件和