Sun developed the JAXP (Java API for XML Processing) API to allow our applications to access XML documents without relying on the specific parser.
JAXP does not have a new function to expand the parser. It is an encapsulation of the parser, allowing developers to be independent from the specific parser, in this way, we can replace the parser in the application without changing the application code. Currently, mainstream Resolvers support DOM and sax, so JAXP also supports them. The current version of JAXP is 1.3 included in JDK.
Javax. xml package and sub-package, org. W3C. Dom package and sub-package, org. xml. Sax package and sub-package. Javax. the XML package and sub-package mainly obtain the parser instance. After obtaining the parser instance, you can read the XML. If we use Dom parsing, we will use Org. w3C. the interfaces or classes in the DOM package and sub-packages. XML. interfaces or classes in the sax package and sub-package. How can I get a parser instance through JAXP?
The javax. xml. parsers package provides four types of documentbuilder, documentbuilderfactory, saxparser, and saxparserfactory. The first two are the DOM parser instances, and the last two are the SAX Parser instances.
1. Get the DOM parser instance
Java code
- Package test;
- Import java. Io .*;
- Import javax. xml. parsers .*;
- Import org. W3C. Dom. Document;
- Public class jaxptest {
- Public static void main (string [] ARGs ){
- Try {
- Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
- Documentbuilder builder = factory. newdocumentbuilder ();
- Document document = builder. parse (New fileinputstream ("My. xml "));
- } Catch (exception e ){
- E. printstacktrace ();
- }
- }
- }
The Document Object of the above Code is the XML document tree, where the XML document data is located.
Ii. Change the parser
It is easy to change the parser used by the JAXP factory class. Changing the parser actually means changing the parser factory because all saxparser and documentbuilder instances come from these factories. The factory determines which parser to load, so the factory must be changed. To change the implementation of the saxparserfactory interface, set java system features javax. xml. parsers. saxparserfactory. If this feature is not defined, the default implementation is returned (no matter which parser the developer specifies ). The same rule applies to the used documentbuilderfactory implementation. In this case, the system features of javax. xml. parsers. documentbuilderfactory will be queried.
Iii. How to load the parser for JAXP APIs
1. Use System Properties
If we set the system Attribute before calling documentbuilderfactory factory = documentbuilderfactory. newinstance (); the Code is as follows:
System. setproperty ("javax. xml. parsers. documentbuilderfactory", "org. Apache. xerces. JAXP. documentbuilderfactoryimpl ");
Then JAXP will use the parser you provided. You can also set System Properties by using the-D parameter when running this class, for example, Java-djavax. XML. parsers. documentbuilderfactory = org. apache. xerces. JAXP. documentbuilderfactoryimpl coresun.cn. jaxptest
2. Create the JAXP. properties file in the java_home \ JRE \ Lib folder and add the following content to the file:
Javax. xml. parsers. documentbuilderfactory = org. Apache. xerces. JAXP. documentbuilderfactoryimpl
3. Find the META-INF \ Services Directory of the parser JAR File
If the javax. xml. parsers. documentbuilderfactory file is contained in this directory, the parser is loaded through the content of this file.
4. If no parser is found in the first three methods, jaxb uses the default parser Apache xerces (JAXP 1.1 is bound with Apache crimson ).
Below is an interesting article about JAXP, xml api reflects sun and IBM's hatred, for reference, original address: http://www.w3china.org/blog/more.asp? Name = hongrui and ID = 23698
The original XML Parser cited was Sun's crimson and IBM's xerces, both of which were donated to the Apache organization. Later, xerces developed rapidly and crimon was rarely used.
Java APIs for XML processing have been added to Java 2 Since version 1.4. This API can be used to process XML documents through a series of standard Java platform APIs.
Therefore, you do not need to add an XML Processing Package. Sun's jdk1.4 uses crimson and IBM's JDK uses xerces.
If you want to port Sun's JDK program to IBM's JDK, note that the parser is different and XML processing will fail. This shows that Java is not "one compilation, running everywhere", but "one compilation, debugging everywhere ".
If you port the ibm jdk to Sun's JDK, even if you introduce the xerces package to classpath, JDK still uses Crimson. Do not believe you have to run Java-verbose for a try.
The solution is to create a JAXP. properties file under the JRE \ Lib \ directory,
The content is as follows:
Javax. xml. parsers. documentbuilderfactory = org. Apache. xerces. JAXP. documentbuilderfactoryimpl
Javax. xml. parsers. saxparserfactory = org. Apache. xerces. JAXP. saxparserfactoryimpl
Xerces can be used.
Or use the command line
# Add the XML Parser jars and set the JAXP factory names
# Crimson parser JAXP setup (default)
Classpath = $ classpath: ../lib/Crimson. Jar
JAXP =-djavax. xml. parsers. documentbuilderfactory = org. Apache. Crimson. JAXP. documentbuilderfactoryimpl
JAXP = "$ JAXP-djavax. xml. parsers. saxparserfactory = org. Apache. Crimson. JAXP. saxparserfactoryimpl"
Or
# Add the XML Parser jars and set the JAXP factory names
# Xerces parser JAXP setup
Classpath = $ classpath: ../lib/xerces. Jar
JAXP =-djavax. xml. parsers. documentbuilderfactory = org. Apache. xerces. JAXP. documentbuilderfactoryimpl
JAXP = "$ JAXP-djavax. xml. parsers. saxparserfactory = org. Apache. xerces. JAXP. saxparserfactoryimpl"
Sun finally specified the JAXP specification, JSR 206 Java API for XML Processing (JAXP) 1.3, which has been implemented by jdk1.5.
JAXP implements XPath, but the org. Apache. XPath. xpathapi class of xalan has been transplanted to JRE 1.5 and reconstructed to com.sun.org. Apache. XPath. Internal. xpathapi.
If you use a jar containing the xpathapi class in the previous JDK, such as a xalan-2.4.1.jar. Add the jar to the classpath (class path ).
W3C standard DOM APIs are very difficult to use, so some people develop Java-specific XML APIs, which are JDOM = Java + DOM. Some of them develop dom4j, which is not very standard but fast.
The use of JDOM implies server risks. Because JBoss and webshells are both developed based on JDOM, JDOM must be configured for these two servers, and your JDOM version must be similar to that used by the server,
Because the JVM only loads the same class, the server first loads the JDOM you use. The JDOM you use will not be loaded, and your application will encounter errors.
The implicit engineering problem with dom4j is mainly hibernate. If your development project and Hibernate are merged and the dom4j version is incompatible, find the same version of Hibernate with dom4j, or you can change the program to adapt to the hibernate dom4j.
Java does not have windows DLL hell, but Java jar hell has never been inferior. JAXP comprehensive introduction to http://www.ibm.com/developerworks/cn/xml/x-jaxp/index.htmlhttp://www.ibm.com/developerworks/cn/xml/x-jaxp2/