powered by Jive Software

Getting a XmlPullParserFactory from KXML

I’‘m trying to get Smack to work with KXML (http://xmlpull.org/v1/doc/api/index.html), but I run into the problem that I can’'t instantiate an XmlPullParserFactory. Originally Smack uses:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance(“org.xmlpull.mxp1.MXParserFactory”, null);

but KXML doesn’'t have an ‘‘MXParserFactory’’ so I use the following instead:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance(“org.xmlpull.v1.XmlPullParserFactory”, this.getClass());

I assume that the XmlPullParserFactory is a fitting factory. The error this produces is:

Exception: org/xmlpull/v1/XmlPullParserException

org.xmlpull.v1.XmlPullParserException: No valid parser classes found in parameter classNames to newInstance() that contained ‘‘org.xmlpull.v1.XmlPullParserFactory’’

at org.xmlpull.v1.XmlPullParserFactory.newPullParser(+76)

at org.jivesoftware.smack.PacketReader.(+107)

at org.jivesoftware.smack.XMPPConnection.init(+98)

at org.jivesoftware.smack.XMPPConnection.(+305)

at org.jivesoftware.smack.XMPPConnection.(+8)

XMLPull’'s API states the following:

“name of parser class factory to use and its class used for loading (no classloader - on J2ME no access to context class loaders) must be passed explicitly. 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”

So it seems that the VM can’‘t find ‘‘org.xmlpull.v1.XmlPullParserFactory’’, although it IS in the classpath. I have no idea how I can avoid this error. I’‘ve tried setting a different Class as context but that didn’'t solve the problem.

Any suggestions?




I really don’'t have much experience working with xml pull parsers so take my advice “under your own risk”.

Having said that, KXML does not provide a Factory for optimization reasons (I think) so you can avoid using a factory and directly create a new instance of org.kxml2.io.KXmlParser and assign it to the parser variable.


– Gato

Hi Gato,

Again you are right! I already tried your solution and it turned out to be correct. Instead of:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance(“org.xmlpull.v1.XmlPullParserFactory”, this.getClass());

I now use:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance(“org.xmlpull.v1.XmlPullParser”, this.getClass());

The xmlpull documentation is not very clear that you can’'t specify the parserfactory itself, instead you have to specify the parser.