java.lang.ClassNotFoundException:org.apache.xerces.parsers.SAXParser

用SAX解析XML.

String vendorParserClass= "org.apache.xerces.parsers.SAXParser";

XMLReader reader = XMLReaderFactory.createXMLReader(vendorParserClass);

抛出异常:java.lang.ClassNotFoundException:org.apache.xerces.parsers.SAXParser

原因:未在classpath中加入xercesImpl.jar.

但如果改为:XMLReader reader = XMLReaderFactory.createXMLReader();

程序能正常运行。此时程序创建的是一个默认的XMLReader。


如果系统的org.xml.sax.driver i没有被指定为特定的service API:org.xml.sax.driver , org.xml.sax.helpers.此时XMLReaderFactory.createXMLReader 将会使用由SAX Parser指定的默认的XMLReader class (在SUN JDK5, 默认的类是com.sun.org.apache.xerces.internal.parsers.SAXParser.)因此,如果应用程序改变了系统的 org.xml.sax.driver 属性,指向了org.apache.xerces.parsers.SAXParser, 将会产生ClassNotFoundException.

如果不添加xercesImpl.jar的解决方法是:在程序中如果要获取 XMLReader,则不要设定系统的org.xml.sax.driver 属性,或者使用如下的回退机制:

catch (Exception e) { 
                  try { 
                       // If unable to create an instance, let's try to use 
                       // the XMLReader from JAXP 
                       if (m_parserFactory == null) { 
                           m_parserFactory = SAXParserFactory.newInstance(); 
                           m_parserFactory.setNamespaceAware(true); 
                       }

                       reader = m_parserFactory.newSAXParser().getXMLReader();


Note :that it is generally not a good idea to hard code a reference com.sun.org.apache.xerces.internal.parsers.SAXParser in your application, because the class might not be available when JDK upgrades or in other distributions of JDK .



依赖的包:xml-apis.jar xercesImpl.jar

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值