Java XML API

xiaoxiao2021-03-06  101

Author: robbin (MSN: robbin_fan AT hotmail DOT com) has two excellent review article on Java XML API on IBM's developerWorks: performance in Java Java XML document models in the use of XML document model these two articles What I want to say is to vomit blood recommend Java's XML API. These articles should be told, I just want to add a few points: 1. Crimson and Xerces moils

CRIMSON has from Sun donated to apache's ProjectX project, Xerces donated to apache's XML4J project, and Xerces won, became the XML API developed by Apache XML team, and Crimon didn't do it, now Xerces is full of world. They are all in the Xerces Dom and SAX parsers. Only Sun is not convinced, but it is necessary to use CRIMSON in JDK1.4, which makes people feel like it is, it is really pitiful! However, the XML parser for IBM release JDK is naturally Xerces.

Due to the priority relationship of JDK Class Loader, when you use JAXP to write an XML program, even if the Xerces package introduces ClassPath, JDK still uses Crimson, which can be observed by opening the JVM's Verbose parameter. However, JDK also allows you to use other parsers, so we can replace the parser by built a JAXP.Properties in the JRE / LIB / directory, JAXP.Properties content is as follows:

Javax.xml.parsers.documentbuilderfactory = org.apache.xerces.jaxp.documentBuilderFactoryImpl

Javax.xml.parsers.saxparserfactory = org.apache.xerces.jaxp.saxparserfactoryImpl

This will use Xerces, of course, you must still put the Xerces to ClassPath.

Second, JAXP is late

The Sun is always known in the XML field. When Sun is looking at XML, XML's API has fly over the sky, especially IBM has a very big leading advantage. However, Sun is a standardized settlement, so the JAXP specification is developed with reference to W3C. JAXP is not like Xerces and Crimon, it is just a spec. It is not anything. Its role is to make a unified interface, let other XML APIs follow JAXP programming, then use JAXP written procedures, The base layer API can be switched.

Specifically, JAXP includes several factory classes, which is the javax.xml.parsers package inside JDK1.4 to find the location of the XML API implementation class in accordance with the DOM standard; in addition, JAXP also includes a set of Interface, this is JDK1 .4 Org.w3c.dom in the .4. Factory class is responsible for loading the implementation class of the DOM. So what is the rule loaded?

I know by reading JAXP's source code, the factory class will be found according to the parameters incorporated by the Java command line, and then find it in the implementation class defined in jre / lib / jaxp.properties, the last thing I can't find. Just use crimson. Note that Crimons is from Bootstrap Class Loader, if you don't change the factory's search order, I will use Crimson :(

Third, the DOM parser and the DOM API are when you strictly use JAXP programming, it is followed by the W3C's DOM standard, then you can actually switch different DOM implementations, such as Xerces, or CRIMON, or other, or other, or other, The switching method is to configure jaxp.properties. So JAXP is some standard interfaces.

Xerces and Crimon are not just a DOM implementation, and they actually include the SAX parser and the DOM parser. So a JAXP program has the following level:

JAXP Application -> JAXP Interface -> Xerces DOM Implementation -> Xerces DOM / SAX Resolution

As long as you use JAXP programming, you can switch to Crimson.

JAXP Application -> JAXP Interface -> Crimson DOM Implementation -> Crimson DOM / SAX Resolution

You can also do this:

JAXP Application -> JAXP Interface -> Crimson DOM Implementation -> Xerces DOM / SAX Resolution

However, if your program does not install JAXP, then there is no way to switch different DOM implementations.

Fourth, not the standard DOM4J and JDOM

The W3C's DOM standard API is hard to use, so there is a group of people to develop Java-specific XML API purposes to make it easy, this is the origin of JDOM, when they developed to half, another part of people have divided it, they Have your own ideas, so they will develop DOM4J, forming two APIs today, as for the performance between them, the function is more likely to know that the article I recommend above is known, JDom is full of defeat.

JDOM is equivalent to the top of the JAXP interface Xerces Dom implementation, which does not have a parser, which can use Xerces or Crimson's parsers, that is:

JDOM App -> Jdom API -> Xerces / CRIMSON parser

DOM4J and JDOM are similar, but he binds a parser called Alfred2, the function is not very full, but the speed is very fast, and the DOM4J will use the Alfred2 parser when there is no other parser, as follows:

DOM4J app -> DOM4J API -> XERCES / CRIMSON parser

or

DOM4J app -> DOM4J API -> ALFRED2 parser

Dom4j.jar, which is downloaded on the sf, is free of an Alfred2 parser, and the DOM4J-FULL.jar contains the Alfred2 parser. In this case, you don't need anything, just a DOM4J-FULL. JAR is all included.

It can therefore be seen that applications written in DOM4J / JDOM do not have portability.

Five, small episodes

Sun is the JAXP standard servant, even very persistently binds Crimson Dom implementation and parser in JDK1.4, then coming, Sun own JAXM RI is not written in JAXP, but Dom4j, develop standard Let everyone obey, but you have to supervise theft, this is not too much to say it!

BTW: hibernate also uses DOM4J to read XML configuration files, and now more and more procedures have used DOM4J, if you care about portability, I strongly recommend that you use DOM4J.

转载请注明原文地址:https://www.9cbs.com/read-100391.html

New Post(0)