JAVA parsing XML,DTD Verification Problem __java

Source: Internet
Author: User

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.

Related Article

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.