Jaxp.properties file issues when parsing XML in Java

Source: Internet
Author: User
Tags xml parser

At work, using Java's JAXP to read parsing XML files, you encounter a singular thing. In Web engineering, debugging finds that JAXP is actually using the Xerces parser,
However, when a small swing tool in the project is used as a jar package in the same way as the web, debugging finds that JAXP actually uses crimson
Parser, also found parsing XML file error, after analysis, found that the Crimson parser was developed by Sun (the actual use found crimson no xerces parser stability),
Packaged in the Java_home/lib/dt.jar package, this dt.jar is set in the environment variable classpath, and when the executable jar is run, JAXP uses the CLASSPATH parser.
The war package in the Web project is not.
By reading JDK source Javax.xml.parsers.factoryfinder,javax.xml.parsers.saxparserfactory and Documentbuilderfactory find the JDK in the following order:

1 System Properties Javax.xml.parsers.DocumentBuilderFactory or Javax.xml.parsers.SAXParserFactory

2 Javax.xml.parsers.DocumentBuilderFactory or Javax.xml.parsers.SAXParserFactory properties set in Jdk-dir/lib/jaxp.properties

3 values set in Meta-inf/services/javax.xml.parsers.documentbuilderfactory or javax.xml.parsers.SAXParserFactory files in the runtime jar package

4. If none of the above parsers are found, use crimson. If not yet ... The newspaper ClassNotFound out of the ordinary.

By JAXP to find the order of the parser, we can use the following method to determine the actual parser that we use,
1 Write dead actual parser in the program
Such as
Javax.xml.parsers.DocumentBuilderFactory factory= new Org.apache.crimson.jaxp.DocumentBuilderFactoryImpl ();
2 using Jaxp's Documentbuilderfactory factory class, such as
Javax.xml.parsers.DocumentBuilderFactory factory= javax.xml.parsers.DocumentBuilderFactory.newInstance ();
The actual parser class is then specified in the following way
Method One: When running Java, by setting java-d javax.xml.parsers.documentbuilderfactory=new Org.apache.crimson.jaxp.DocumentBuilderFactoryImpl
Method Two: Call System.setproperty in the program ("Javax.xml.parsers.DocumentBuilderFactory", " Org.apache.crimson.jaxp.DocumentBuilderFactoryImpl ")
To set the actual XML parser.
Method Three: Write a jaxp.properties file, in which the following content is added
Javax.xml.parsers.documentbuilderfactory=org.apache.crimson.jaxp.documentbuilderfactoryimpl
Then put this file into the java_home/lib/
Method Four: Under the JAR package, create a new services directory under directory meta-inf/, create a new file named Javax.xml.parsers.DocumentBuilderFactory in this directory,
The file content is written on the parser class that is actually used, such as write Org.apache.crimson.jaxp.DocumentBuilderFactoryImpl
Through, above, we can have a relatively deep understanding of JAXP. In fact, there are many such ideas in Java, this kind of thinking, refers to what is platform-independent, development to not rely on the implementation of concrete.
such as the JNDI,JDBC,JAXP we are familiar with. Jndi is a class library that smokes a variety of directory service operations, because there are too many directory server vendors, such as Sun's ldapsdk, and Novell and so on. JDBC is pumped like a variety of databases
Operation of the class library, because there are too many database vendors, such as Oracle,sqlserver,mysql,informix and so on, JAXP is to smoke like a variety of XML Parser and converter product class library, because the XML parser and converter products enough
of the.

Accessing the Web that is deployed in oc4j on AIX systems The module's page encountered this error:
Javax.xml.parsers.FactoryConfigurationError:Provider null could not being instantiated:
Java.lang.NullPointerException
at javax.xml.parsers.SAXParserFactory.newInstance (Unknown Source)
at Org.apache.commons.digester.Digester.getFactory (digester.java:478)
at Org.apache.commons.digester.Digester.getParser (digester.java:683)
at Org.apache.commons.digester.Digester.getXMLReader (digester.java:891)
at Org.apache.commons.digester.Digester.parse (digester.java:1591)
at Org.apache.struts.action.ActionServlet.initServlet (actionservlet.java:1433)
at Org.apache.struts.action.ActionServlet.init (actionservlet.java:466)
at Javax.servlet.GenericServlet.init ( genericservlet.java:256)
at com.evermind[oracle Containers for Java EE 10g (10.1.3.0.0)]. Server.http.HttpApplication.loadServlet (httpapplication.java:2231)
at com.evermind[oracle Containers for EE 10g (10.1.3.0.0)].server.http.httpapplication.findservlet (httpapplication.java:4617)
...
or such an exception:
javax.servlet.jsp.JspException:Can ' t get definitions factory from context.
At Org.apache.struts.taglib.tiles.InsertTag.processDefinitionName (inserttag.java:583)
At Org.apache.struts.taglib.tiles.InsertTag.createTagHandler (inserttag.java:487)
At Org.apache.struts.taglib.tiles.InsertTag.doStartTag (inserttag.java:451)
At _welcome._jspservice (_welcome.java:54)
[Src:/welcome.jsp:4]

...
and when deployed in a Windows Environment, there is no problem.

This is because IBM Aix ibm JDK ibm and Sun jdk specifically because $JAVA _home/jre/lib/ Jaxp.properties this file.

This file with Key=value form configuration and specify the actual use of the Xml Parser Implementation class ( For example: Javax.xml.parsers.saxparserfactory=org.apache.xerces.jaxp.saxparserfactoryimpl) xml Before the parser is initialized, jdk first search system< Span style= "font-family: the song Body;" > properties Look for the parser configuration item, if not, search $JAVA _home/jre/lib classpath . Jar

In fact, this file does not exist in SUN 's JDK . In the case where the file is not found, the default parser will eventually be used. the jaxp.properties file exists in IBM 's JDK . However, all the default configuration items in this file are commented out, so when we search here, we no longer continue to search down, but because the configuration item is not read, it will return null , so the first error has occurred. The second exception is probably because when you access a page that uses the tiles framework, the configuration file for the tiles framework cannot be read because of an error in the initialization of the XML parser .

Jaxp.properties file issues when parsing XML in Java

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.