XMLPullParser在执行时抛出错误

时间:2012-07-28 17:58:48

标签: java xmlpullparser

我尝试了一个使用XMLPullParser的简单示例,但它抛出异常。

Exception in thread "main" org.xmlpull.v1.XmlPullParserException: could not load any factory class (even small or full default implementation); nested exception is: 
org.kxml2.io.XmlReader
at org.xmlpull.v1.XmlPullParserFactory.newInstance(XmlPullParserFactory.java:225)
at org.xmlpull.v1.XmlPullParserFactory.newInstance(XmlPullParserFactory.java:76)
at XMLParsing.main(XMLParsing.java:18)

我的代码是:

import java.io.IOException;
import java.io.StringReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class XMLParsing {

    /**
     * @param args
     * @throws XmlPullParserException 
     * @throws IOException 
     */
    public static void main(String[] args) throws XmlPullParserException, IOException {

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        xpp.setInput(new StringReader ("<foo>Hello World!</foo>"));
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
         if(eventType == XmlPullParser.START_DOCUMENT) {
             System.out.println("Start document");
         } else if(eventType == XmlPullParser.END_DOCUMENT) {
             System.out.println("End document");
         } else if(eventType == XmlPullParser.START_TAG) {
             System.out.println("Start tag "+xpp.getName());
         } else if(eventType == XmlPullParser.END_TAG) {
             System.out.println("End tag "+xpp.getName());
         } else if(eventType == XmlPullParser.TEXT) {
             System.out.println("Text "+xpp.getText());
         }
         eventType = xpp.next();
        }

    }

}

有人可以说可能是什么问题吗?

1 个答案:

答案 0 :(得分:0)

我认为异常是抛出的,因为您没有传递基于必须创建工厂的类名。尝试使用其重写的方法newInstance(java.lang.String factoryClassName)

您可以在此处找到有关使用工厂的更多信息:http://www.xmlpull.org/v1/doc/api/org/xmlpull/v1/XmlPullParserFactory.html#newInstance%28%29