Jaxp Comprehensive Introduction, part 1th

Source: Internet
Author: User
Tags error handling object model xml parser

Java technology and XML are undoubtedly the most important programming development tools in the last five years. Therefore, the API for processing XML in the Java language has developed. The two most popular--Document Object Model (DOM) and simple API for XML (SAX)--have had a huge impact, and the JDOM and data binding APIs have emerged. It is necessary to thoroughly understand one or two APIs, and proper use of all APIs will make you authoritative. However, more and more Java developers find that they no longer need to have a broad understanding of SAX and dom--, which is mainly due to the JAXP Toolkit of Sun Microsystems. Java API for XML processing (JAXP) makes XML easy to master even for Java primary developers, and greatly improves the capabilities of advanced developers. That is, even advanced developers who use JAXP have misconceptions about the APIs they rely on very much.

This article assumes that you have a basic understanding of SAX and DOM. If you don't understand XML parsing at all, you might want to first read the information in online resources about SAX and DOM, or browse through my book. You do not need to be proficient in callback or Dom Node, but you must at least understand that SAX and Dom are parsing APIs. This article also helps to understand the differences between them in general. If you have mastered these basics, this article will be more helpful to you.

JAXP: Is it an API or an abstraction?

Strictly speaking, JAXP is an API, but more precisely the abstraction layer. It does not provide a new way to parse XML, is not added to SAX or DOM, and does not provide new functionality for Java and XML processing. (If you don't believe it yet, it's right to read this article.) However, JAXP makes it easier to use DOM and SAX to handle some difficult tasks. It also allows developers to handle some developer-specific tasks that may be encountered when using the DOM and SAX APIs in a developer-neutral manner.

Progressive Promotion

In earlier versions of the Java platform, JAXP was a separate download in the core platform. In Java 5.0, JAXP is already the main product of the Java language. If you already have the latest version of JDK, you've got JAXP.

XML cannot be parsed without SAX, DOM, or another XML parsing API. I've seen a lot of requests to compare SAX, DOM, JDOM, and dom4j with JAXP, but this comparison is not possible because the first four APIs have completely different uses for JAXP. The XML is parsed by SAX, DOM, JDOM, and dom4j. JAXP provides a way to reach these parsers and the data involved, but does not provide a new way to parse XML documents. It is essential that you understand this difference if you want to use JAXP correctly. This is also likely to lead you far ahead of your XML development peers.

If you still have questions, make sure you have a JAXP release. Launches a Web browser and loads the JAXP API documentation. Navigate to the parsing section of the API that is located in the Javax.xml.parsers software package. Surprisingly, you will find only six classes. What's wrong with this API? All of these classes are located at the top of the existing parser. Two of these classes are used only for error handling. JAXP is much simpler than people think. So why is there confusion?

At the top of

Even JDOM and dom4j are at the top of other parsing APIs, just like JAXP. But these two APIs provide different models for accessing data from sax or DOM, using sax internally (with some tricks and modifications) to reach the data they provide to the user.

Sun's JAXP and Sun's parser

Many parser/api confuse the parser from the Sun package Jaxp and the JAXP default use. In the earlier version of JAXP, Sun included the Jaxp API (with the six classes mentioned earlier and some classes that are commonly used for conversion) and a parser called crimson. Crimson is part of the Com.sun.xml software package. In a new version of JAXP-including the JDK,--sun has repackaged the Apache Xerces parser. In both cases, although the parser is part of the JAXP release, it is not part of the JAXP API.

It can be thought that JDOM comes with the Apache Xerces parser. The parser is not part of the JDOM, but is used by JDOM, so it is included to ensure that JDOM can be installed. The same principle applies to JAXP, but it is not explicitly advertised: Jaxp comes with a parser for immediate use. However, many people use the classes included in the Sun's parser as part of the JAXP API itself. For example, frequently asked questions on newsgroups are "How do I use the XmlDocument class that comes with JAXP?" What is the role of it? "The answer is a little complicated.

What is in the package name?

When I first opened the source code in Java 1.5, I was amazed at what I saw--or more, what I didn't see. Xerces was not found in the normal package org.apache.xerces because Sun reassigned the Xerces class to com.sun.org.apache.xerces.internal. (I found it a little unusual, but nobody asked me.) In any case, if you look for Xerces in the JDK, you can find it.

First, the Com.sun.xml.tree.XMLDocument class is not part of JAXP. It is part of Sun's Crimson parser, packaged in an earlier version of JAXP. So the question was misleading from the start. Second, the main purpose of JAXP is to provide developer independence when dealing with parsers. With JAXP, you can use the Sun's XML parser, the Apache Xerces XML parser, and the Oracle XML parser to handle the same code. So using Sun-specific classes violates the point of using JAXP. Have you figured out how this topic gets complicated? The APIs and parsers in the JAXP release are grouped together, and some developers mistakenly use classes and attributes in the parser as part of the API, and vice versa.

Now that you've figured out all the confusion, you'll be able to delve into some of the code and concepts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.