Exception in thread "main" org.xmlpull.v1.XmlPullP

[xmlpull]XmlPull常见错误

编写者

日期

关键词

郑昀@ultrapower

2005-9-28

Xmlpull kxml java

 

Xmlpull官方站点:http://www.xmlpull.org/

优点:不必等整个文档解析完成,部分求值结果早就可以开始反馈给用户。

What Is It?

XmlPull project is dedicated to be a site for

  • for general Pull parsing promotion/education (including StAX) and in particular to contain easy-to-reuse samples and code fragments

  • a resource for discussing new ideas and concepts related to pull parsing

  • a java namespace (org.xmlpull.*) and project location of a free implementation of the event object API and Factories, based on the StAX XMLStreamReade and old Common API for XML Pull Parsing

  • as java namespace and project location of StAX and XmlPull based utilities and samples such as:
    - providing an XML stream from a DOM tree
    - a DOM builder
    - SAX adapter
    - JUnit tests

  • as a maintenance resource for the existing XmlPull interface

常见错误一:XmlPullParserException:resource not found

网络中有很多人问到同样一个问题:

为什么我们在调用

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

时,总是得到这样的错误:

 错误提示:

Exception in thread "main" org.xmlpull.v1.XmlPullParserException: caused by: org.xmlpull.v1.XmlPullParserException: resource not found: /META-INF/services/org.xmlpull.v1.XmlPullParserFactory make sure that parser implementing XmlPull API is available

    at org.xmlpull.v1.XmlPullParserFactory.newInstance(XmlPullParserFactory.java:294)

虽然从http://www.xmlpull.org/v1/doc/api/org/xmlpull/v1/XmlPullParserFactory.html

看到了他们自己的注解:

 XmlPullParserFactory的注释:

If no name of parser factory was passed (or is null) it will try to find name by searching in CLASSPATH for META-INF/services/org.xmlpull.v1.XmlPullParserFactory resource that should contain a comma separated list of class names of factories or parsers to try (in order from left to the right). If none found, it will throw an exception. 
NOTE:In J2SE or J2EE environments, you may want to use newInstance(property, classLoaderCtx) where first argument is System.getProperty(XmlPullParserFactory.PROPERTY_NAME) and second is Thread.getContextClassLoader().getClass() .

看来它确实需要寻找这么一个资源:

META-INF/services/org.xmlpull.v1.XmlPullParserFactory

。但又没说如何才能找到它。

解决办法

http://kxml.sourceforge.net/

下载的kxml2.jar加到你的项目中即可。这时候再调用

XmlPullParserFactory factory = XmlPullParserFactory.newInstance(

                            System.getProperty(XmlPullParserFactory.PROPERTY_NAME),

                   Thread.currentThread().getContextClassLoader().getClass() );

就通过了。

kxml2.jar就包含了META-INF/services/org.xmlpull.v1.XmlPullParserFactory文件,它的内容其实就是一句话:

org.kxml2.io.KXmlParser,org.kxml2.io.KXmlSerializer


转载于:https://my.oschina.net/u/1157906/blog/222337

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值