0

我尝试了一个使用 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();
        }

    }

}

谁能说可能是什么问题?

4

1 回答 1

0

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

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

于 2012-07-28T19:19:04.297 回答