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

2014/04/15 11:47
阅读数 1.3K

[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


展开阅读全文
打赏
0
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部