Question

I have trying out a simple example using XMLPullParser but it is throwing an exception.

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)

My Code is:

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();
        }

    }

}

Can anyone say what might be the problem?

Was it helpful?

Solution

I think the exception is throws as you're not passing a class name based on which the factory has to be created. Try using its overridden methods newInstance(java.lang.String factoryClassName) .

You can find more info on using the factory here : http://www.xmlpull.org/v1/doc/api/org/xmlpull/v1/XmlPullParserFactory.html#newInstance%28%29

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top