XmlResourceParser implementation

1.4k views Asked by At

I need to dynamically load a xml layout from the server. LayoutInflater has inflate methods that use a XmlPullParser. I've tried that, but it doesn't work.

Looking into the Android source code, it turns out those inflate methods are called with a XmlResourceParser. The implementation Android uses is XmlBlock.Parser, but that is not a public API.

Is there a XmlResourceParser public implementation I can use?

4

There are 4 answers

0
jmarranz On

YES, right now is possible with ItsNat Droid.

Take a look to this summary:

https://groups.google.com/forum/#!topic/itsnat/13nl0P12J_s

It is still under heavy development but most important features are already implemented.

2
afpro On

in fact, you CAN NOT load xml layout dynamically. android system DOES NOT need a XmlResourceParser. when android ui system inflate an resource, it just convert the parser to it's private implementation, a binary xml source parser (i forgot the class name).

1 year ago, i tried this, spent many many times. so, don't waste your time as me again.

7
Antoine Marques On

You can use a traditional XmlPullParser like described in Android documentation :

InputStream yourRemoteLayout = ...;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(yourRemoteLayout, "someEncoding");
AttributeSet attributes = Xml.asAttributeSet(parser);

Please see what's explained in XmlPullParser documentation for more details.


Edit : From LayoutInflater#inflate() documentation :

Important   For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

What I guess, is that maybe you should make your own implementation of LayoutInflater.Factory2 if Android's own only rely on preprocessed resources.

1
FunkTheMonk On

For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

That isn't to say it can't be done. But you will need to run the build tools on the xml file to get it into the right format. Then you can probably mock a 'Context' and 'Resources' that returns the downloaded data when used in a 'LayoutInflator'