I want to use external(like /storage/sdcard0/main.xml) xml as Layout by LayoutInflater.
So I found use external xml by XmlPullParser. But It didn't work!!
How to write xml source which is operating on XmlPullParser and LayoutInflater?
LayoutInflater does not work on arbitrary xml at runtime. See the comment on LayoutInflater.inflate.
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.
The platform performs a downcast in Resources.java that makes this assumption.
// XXX note that for now we only work with compiled XML files.
// To support generic XML files we will need to manually parse
// out the attributes from the XML file (applying type information
// contained in the resources and such).
XmlBlock.Parser parser = (XmlBlock.Parser)set;
Related
I have a desktop application that produces resource / data files for my android app. These are XML text files that store instances of my custom data class. These objects are serialized using the Simple XML Serialization library. In my android app, I'd like to instantiate objects from this XML serialization class.
I like to add these xml files to Android Studio so they are included in the APK on device install and are placed, for example, in the private app directory "files", to which getFilesDir() is mapped. I can't find a way to do that.
If I add these xml files to the Android XML resource folder, I need to use Android's XML resource parser, and can not use the Simple XML library.
Any tips? I feel I made a wrong design choice seeing how restrictive the resource bundling is.
Thanks, Kind regards,
Harmen
As per CommonsWare's comment: the solution was adding it to the raw resource folder, then you can access it using:
InputStream xmlExerciseInputStream = getResources().openRawResource(R.raw.myresource);
MyClass myClass = serializer.read(MyClass.class, xmlExerciseInputStream);
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?
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.
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.
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.
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'
I have hardcoded data in my Android application. This will be in tree format. like this
<makes>
<make>
<name>abc</name>
<id>1</id>
</make>
... 10 more entries.
</makes>
Since this data will be hard coded, in my application, what can i use to easily parse in in my application. I can simply load it from my assets directory, and use xml parsing, but is there any inbuilt ways to use it. I know i can make a String array from xml resource, but how can i use this structure.
You could easily store that table as an SQLite database. This link might be helpful, particularly step 6.
You can use the XmlResourceParser class, that implements the XmlPullParser interface, and will allow you to parse the XML file, loading it from the resources folder. After that, I would recommend defining a Make class with the two properties defined in the XML and populate a List or other type of data structure with objects of that class while you loop through the xml file.
I am new to Android development and I am wanting to parse a node only xml file using XML Resource Parser in android. I can only get it to parse an XML that contains attributes however I dont want my xml to contain attributes and I just want to pull out data contained in nodes only. Can I do this with XRP? There seem to only be methods for XML's containing attributes i.e. getAttributeValue for example
My xml looks like this:
<Streets>
<street>ABBEY WAY</street>
<district>WILLESBOROUGH</district>
<collectionday>FRIDAY</collectionday>
<jan>6,20</jan>
<feb>3,17</feb>
<mar>2,16,30</mar>
<apr>13,27</apr>
<may>11,25</may>
<jun>8,22</jun>
<jul>6,20</jul>
<aug>3,17,31</aug>
<sep>14,28</sep>
<oct>12,26</oct>
<nov>9,23</nov>
<dec>TBA</dec>
<street>ABBOTT WAY</street>
<district>TENTERDEN</district>
<collectionday>TUESDAY</collectionday>
<jan>10,24</jan>
<feb>7,21</feb>
<mar>6,20</mar>
<apr>3,17</apr>
<may>1,15,29</may>
<jun>12,26</jun>
<jul>10,24</jul>
<aug>7,21</aug>
<sep>4,18</sep>
<oct>2,16,30</oct>
<nov>13,27</nov>
<dec>TBA</dec>
<street>ACKERLEY COURT</street>
<district>STANHOPE</district>
<collectionday>THURSDAY</collectionday>
<jan>12,26</jan>
<feb>9,23</feb>
<mar>8,22</mar>
<apr>5,19</apr>
<may>3,17,31</may>
<jun>14,28</jun>
<jul>12,26</jul>
<aug>9,23</aug>
<sep>6,20</sep>
<oct>4,18</oct>
<nov>1,15,29</nov>
<dec>TBA</dec>
</Streets>
I want to create an array from the street node initially
Many thanks
You could also use Simple for Android. It's very easy and well documented:
http://simple.sourceforge.net/
IMHO a parser where I can get objects directly from a xml document is pretty neat :)
To sum up: in order to add easily unit tests for a SAX parser I would like to load XML from a file.
Now, I have my XML in a static string inside my unit test class, but it is not very convenient for large XML.
This is why I would like to add some XML files to my project and load them in my unit test. How can I do this?
This question is tagged as "Android" and I noticed that you mentioned an Activity, so I'm going to assume that you're trying to load an XML file within an Android application. If that is the case, put your XML file under /assets and call:
InputStream is = getAssets().open("input.xml")
from your Activity. From there, you can manipulate it into SAXBuilder. This will only work if you've set up your test to run on the emulator (or if you're just trying to debug outside of a unit test).
SAXBuilder has a constructor to read data from file:
Document build(java.io.File file)
This builds a document from the supplied filename.
http://www.jdom.org/docs/apidocs/org/jdom/input/SAXBuilder.html