Summary of JAXP Technology

Source: Internet
Author: User
Tags xml parser xslt

This article is a personal conclusion, not a conclusion, so it does not guarantee the correctness of the content of the article. It is only a personal understanding in the course of study...

About JAXP

JAXP is short for Java API for XML processing. In the beginning, jaxp1.0 was called Java API for XML parsing, because JAXP only supports XML parsing at that time, and JAXP continues to evolve, the content supported by the tool is constantly increasing, so it is renamed processing.

JAXP uses the standard parser Simple API for XML parsing (SAX) and Document Object Model (DOM) this allows us to choose between parsing data as event streams or building a Document Object Model. JAXP supports Extensible Stylesheet Language Transformations (XSLT) standards, enables us to convert data to other XML documents or other formats, such as HTML; Starting from jaxp1.4, streaming API for XML (Stax, JSR-173)
Added to the JAXP family.

JAXP and XML syntax parser

Strictly speaking, JAXP is an API, but it is more accurate to call it an abstraction layer. It does not provide a new way of processing XML, does not supplement sax or DOM, and does not provide new functions for Java and XML processing. It only makes it easier to process difficult tasks through DOM and sax. If you encounter vendor-specific tasks when using Dom and sax APIs, it also makes it possible to process these tasks in a vendor-independent manner.

Note that JAXP does not provide syntax analysis! When using JAXP without the APIs for analysis of the sax, Dom, or another XML syntax, you cannot analyze the XML syntax. Although JAXP does not provide syntax analysis, JAXP provides a way to reach the syntax analyzer. JAXP can be seen as a set of specifications, which can use XML parsers from different providers, next we will introduce how to switch to another XML parser.

Currently, JAXP uses the XML syntax analyzer in the Apache xerces project by default in the sax and Dom parsing methods, while the Stax parsing method uses the syntax analyzer provided by Sun by default. These syntax analyzers are not part of the jaxp api. As described above, JAXP does not provide XML syntax analysis, which may be confusing. For example, this is the same as JDOM's xerces syntax analyzer using Apache, but xerces is not part of JDOM.

Do you understand? JAXP APIs and XML syntax analyzers are independent of each other.

About JAXP APIs

It does not explain the specific APIs and how they are used. It only describes the APIS related to each resolution method and how they are organized in JDK, for specific usage, check the API documentation provided by JDK.

The following pictures and content are sourced from Java official website, you can see here: http://docs.oracle.com/javase/tutorial/jaxp/TOC.html

API Overview

  • Javax. xml. parsers: JAXP APIs, which provide common interfaces for different Sax and Dom parsing providers
  • Org. W3C. DOM: Interface related to the Document Object Model (DOM)
  • Org. xml. Sax: defines basic APIs related to sax.
  • Avax. xml. Transform: defines XSLT-related APIs so that we can convert XML into other forms.
  • Javax. xml. Stream: Provides Stax-related APIs.
APIS related to Sax

The outline of the API parsing by using sax:

Overview of relevant sax packages

  • Org. xml. Sax: defines interfaces related to sax.
  • Org. xml. Sax. Ext: defines extension classes for more complex sax processing, such as processing DTD or viewing detailed File Syntax.
  • Org. xml. Sax. helpers: Contains help classes that make the sax application simpler. For example, you can define defaulthandler that contains an empty method.
  • Javax. xml. parsers: defines the saxparserfactory class for returning the saxparser parser. It also defines exceptions used to report resolution errors.

Dom resolution APIs

Dom parsing API outline diagram:

Dom-related package Overview

  • Org. W3C. DOM: defines Dom programming interfaces for XML documents, which are developed by W3C.
  • Javax. xml. parsers: defines the documentbuilderfactory class and documentbuilder class. documentbuilder returns an interface representing W3C document. The parserconfigurationexception class used to report errors is also defined in the package.

XSLT APIs

Xslt api Overview:

XSLT Package Description:

  • Javax. XML. transform: defines the transformerfactory class and transformer class. You can use the transformerfactory class to obtain objects with conversion capabilities. After obtaining the conversion object, call the transform () method using the input (source) and output (result) parameters.
  • Javax. xml. Transform. DOM: class that contains the input and output objects created by Dom
  • Javax. xml. Transform. Sax: class that contains the input object created by the SAX Parser and the output object created by the sax event Processor
  • Javax. xml. Transform. Stream: class that contains the input and output objects created by the I/O Stream

Stax API Introduction

Stax more detailed introduction can see here: http://blog.csdn.net/zhangyihui1986/article/details/8528649

Related Package Description:

  • Javax. xml. Stream: defines the xmlstreamreader interface used to iterate XML document elements and the xmlstreamwriter interface that defines how to write XML
  • Javax. xml. Transform. Stax: APIs.

Comparison of relevant API features of JAXP

XML Parser API feature Summary

Features

Stax

Sax

Dom

Trax

API type

Pull, streaming

Push, streaming

In Memory tree

XSLT rule

Usage of use

High

Medium

High

Medium

XPath capability

No

No

Yes

Yes

CPU and memory efficiency

Good

Good

Varies

Varies

Forward only

Yes

Yes

No

No

Read XML

Yes

Yes

Yes

Yes

Write XML

Yes

No

Yes

Yes

Create, read, update, delete

No

No

Yes

No

JAXP syntax analyzer change

As mentioned above, JAXP is an abstract rather than a specific API. It can use other XML syntax analyzers that comply with the specifications. In fact, switching the syntax analyzer is very simple, that is, switching to the factory class for obtaining syntax analysis, because all saxparser and documentbuilder instances come from these class factories.

You can change the syntax analyzer by setting the java system attributes. You can set the Java System attribute javax. xml. parsers. saxparserfactory to change the implementation of the saxparserfactory interface to be used. If this attribute is not defined, the default implementation is returned. The same principle applies to the documentbuilderfactory implementation. In this case, the system attributes of javax. xml. parsers. documentbuilderfactory will be queried.

You can prove it by reading the relevant JDK source code (using saxparserfactory as an example ):

First, let's take a look at the newinstance method of the saxparserfactory class:

package javax.xml.parsers;// importspublic abstract class SAXParserFactory {    /** The default property name according to the JAXP spec */    private static final String DEFAULT_PROPERTY_NAME = "javax.xml.parsers.SAXParserFactory";    protected SAXParserFactory () {}    public static SAXParserFactory newInstance() {        try {            return (SAXParserFactory) FactoryFinder.find(                /* The default property name according to the JAXP spec */                "javax.xml.parsers.SAXParserFactory",                /* The fallback implementation class name */                "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");        } catch (FactoryFinder.ConfigurationError e) {            throw new FactoryConfigurationError(e.getException(), e.getMessage());        }    }}

Follow the factoryfinder class (this modifier is not public, so it cannot be found in the jdk api documentation) to check the find () method:

Static object find (string factoryid, string fallbackclassname) throws configurationerror {// first, find the implementation class in the system Attribute Based on the factoryid. If yes, instantiate it and return try {string systemprop = ss. getsystemproperty (factoryid); If (systemprop! = NULL) {return newinstance (systemprop, null, true) ;}} catch (securityexception SE) {If (Debug) se. printstacktrace ();} // read $ Java. home/lib/JAXP. properties file. If the key is factoryid, instantiate and return try {string factoryclassname = NULL; If (firsttime) {synchronized (cacheprops) {If (firsttime) {string configfile = ss. getsystemproperty ("Java. home ") + file. separator + "lib" + file. separator + "JAXP. propert Ies "; file F = new file (configfile); firsttime = false; If (ss. doesfileexist (F) {cacheprops. load (ss. getfileinputstream (F) ;}}} factoryclassname = cacheprops. getproperty (factoryid); If (factoryclassname! = NULL) {return newinstance (factoryclassname, null, true) ;}} catch (exception ex) {If (Debug) ex. printstacktrace ();} // try jar service provider mechanic object provider = findjarserviceprovider (factoryid); If (provider! = NULL) {return provider;} If (fallbackclassname = NULL) {Throw new configurationerror ("provider for" + factoryid + "cannot be found", null );} // instantiate the default factory and return newinstance (fallbackclassname, null, true );}

Although this method is long, its structure is clear. According to the newinstance method of the saxparserfactory class, the system first takes javax. XML. parsers. saxparserfactory acts as the key to find the implementation class in the system attribute. If it exists, it is instantiated and returned. If it is not specified in the system attribute, $ Java is read. home/lib/JAXP. properties configuration file. If the key is the attribute value of factoryid, It is instantiated and returned. If no proper factory is found at the end, the default factory is instantiated, here is the second parameter com.sun.org passed in by calling the find method in the facotyrfinder class. apache. xerces. internal. JAXP. saxparserfactoryimpl. It can be seen that Apache xerces syntax analyzer is used by default.

The same analysis can be applied to the documentbuilderfactory class, which is not described here.

In fact, The Stax parsing method is similar, but it is divided into two classes: xmlinputfactory and xmloutputfactory. The corresponding system attribute values are javax. XML. stream. xmlinputfactory and javax. XML. stream. xmloutputfactory, and the default implementation is com. sun. XML. internal. stream. xmlinputfactoryimpl and COM. sun. XML. internal. stream. xmloutputfactoryimpl. Here we can also see that it is Sun's internal syntax analyzer.

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.