加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

applet中解析xml的bug

发布时间:2020-12-16 05:16:27 所属栏目:百科 来源:网络整理
导读:在applet中使用jdk自带的dom或sax解析xml时,会在一定人品的情况下出现下边的错误,主要表现在部分机器能正常,部分机器不正常。错误提示如下 dom报错 javax.xml.parsers.FactoryConfigurationError: Provider !DOCTYPE HTML not found at javax.xml.parsers

在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读