applet中解析xml的bug

前端之家收集整理的这篇文章主要介绍了applet中解析xml的bug前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在applet中使用jdk自带的dom或sax解析xml时,会在一定人品的情况下出现下边的错误,主要表现在部分机器能正常,部分机器不正常。错误提示如下
dom报错
javax.xml.parsers.FactoryConfigurationError: Provider <!DOCTYPE HTML> not found
at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source)
......
sax报错
javax.xml.parsers.FactoryConfigurationError: Provider <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"> not found
at javax.xml.parsers.SAXParserFactory.newInstance(SAXParserFactory.java:134)




查看javax.xml.parsers.DocumentBuilderFactory.newInstance源码
public static DocumentBuilderFactory newInstance() {
try {
return (DocumentBuilderFactory) FactoryFinder.find(
/* The default property name according to the JAXP spec */
"javax.xml.parsers.DocumentBuilderFactory",
/* The fallback implementation class name */
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
} catch (FactoryFinder.ConfigurationError e) {
throw new FactoryConfigurationError(e.getException(),
e.getMessage());
}
}

在applet调用DocumentBuilderFactory.newInstance,会发现它加载<!DOCTYPE HTML>.class这个类,似乎没有正确加载com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl。
代码为:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

解决方法一,直接使用DocumentBuilderFactoryImpl来创建DocumentBuilder
DocumentBuilderFactoryImpl s = new DocumentBuilderFactoryImpl();
DocumentBuilder builder = s.newDocumentBuilder();


解决方法二,把xercesImpl.jar也加进applet中,这个方法同样可以解决问题。

参考资料:
http://stackoverflow.com/questions/9001094/getting-error-provider-com-sun-org-apache-xerces-internal-jaxp-documentbuilderf
http://stackoverflow.com/questions/3085951/problem-with-java-xml-parsing-factoryconfigurationerror-provider-not-found

猜你在找的XML相关文章