questions raised :
Parsing Ejb-jar.xml, the failure occurs when the network is not connected.
Problem Analysis:
We are using the DOM for parsing xml:
Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
Documentbuilder builder = Factory.newdocumentbuilder ();
Location Point
Document doc = builder.parse (file);
Because of the ejb-jar.xml, there are
<! DOCTYPE Ejb-jar Public-//sun Microsystems inc.//dtd Enterprise JavaBeans 2.0//en "" http://java.sun.com/dtd/ejb-jar_2 _0.dtd ">
When parsing, will seize the DTD file from the network HTTP://JAVA.SUN.COM/DTD/EJB-JAR_2_0.DTD, carries on the verification, if the network does not pass, then will appear the resolution failure.
First, Documentbuilderfactory.newinstance () creates the object of the Documentbuilderfactory implementation class, which finds the implementation class in a way:
1. Find key=javax.xml.parsers.documentbuilderfactory in the system environment variable (system.getproperties ())
2. If 1 is not found, then find Java.home/lib/jaxp.properties file, if the file exists, find in the file key=javax.xml.parsers.documentbuilderfactory
3. If 2 is not found, then find meta-inf/services/javax.xml.parsers.documentbuilderfactory files in all the jar packages in Classpath
All failed to find, then return null
If none of the above is found, use the JDK's own implementation class:
Com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
When creating a Documentbuilder instance, there are different implementations based on the difference between the Documentbuilderfactoryimpl.
In order to parse the XML file normally when the network is not available, we can set the entityresolver before using builder:
Builder.setentityresolver (
New Entityresolver () {
Public InputSource resolveentity (string publicid, String systemid) throws Saxexception, IOException
{
return new InputSource (New StringBufferInputStream (""));
Return null;//The effect is still to crawl the DTD from the network to verify
}
}
);
The above settings do not validate the XML file.
If you must verify this, we can also set up a local DTD file to authenticate:
Builder.setentityresolver (
New Entityresolver () {
Public InputSource resolveentity (string publicid, String systemid) throws Saxexception, IOException
{
if (Publicid.equals ("-//sun Microsystems, Inc.//dtd Enterprise JavaBeans 2.0//en"))
{
String Dtd_uri = "C:/TEMP/EJB-JAR_2_0.DTD";
return new InputSource (Dtd_uri);
}
}
);
NOTE: direct return null will still fetch the DTD from the network for verification.