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

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念