I have an XML file:
<building>
<room IMAGE="R.raw.room" />
</building>
but I don't know how to link the value of IMAGE to my main program... When I do this:
[... parsing xml file and detect room ...]
ImageView image = findViewByID(xml.getAttributeValue(0));
it don't works.. Cause it returns me a String and not a int. How can I link this XML file to my resources?
Please help!
Have you tried using getResources().getIdentifier()?
Answered here
R.raw.room is an identifier generated by compiler for a file. You cannot load from XML. What do you want to achieve?
Related
I'm sitting there for quite a while now, trying to process my xml file (similar to the one below). I want to check all tags, if is equal to a variable, and if so, then running readEntry() on the tag.
I followed this example: https://developer.android.com/training/basics/network-ops/xml.html
I also found this Article(Difficulty with XML nested tags with XMLPullParser in Android deals with this topic.
I have already tried a few things but get either nothing or XmlPullParserException.
A Example of my XML:
<VpMobil>
<Kopf>
...
</Kopf>
<FreieTage>
...
</FreieTage>
<Klassen>
<Kl>
<Kurz>5</Kurz>
<Pl>
<Std>
<St>1</St>
<Fa>Fa1</Fa>
<Le>NAME</Le>
<Ra>1009</Ra>
<Nr>131</Nr>
<If/>
</Std>
<Std>
<St>2</St>
<Fa>Fa2</Fa>
<Le>NAME</Le>
<Ra>1004</Ra>
<Nr>132</Nr>
<If/>
</Std>
</Pl>
</Kl>
<Kl>
<Kurz>6</Kurz>
<Pl>
<Std>
<St>1</St>
<Fa>Fa2</Fa>
<Le>NAME</Le>
<Ra>1046</Ra>
<Nr>131</Nr>
<If/>
</Std>
<Std>
<St>2</St>
<Fa>Fa3</Fa>
<Le>NAME</Le>
<Ra>1012</Ra>
<Nr>132</Nr>
<If/>
</Std>
</Pl>
</Kl>
</Klassen>
</VpMobil>
I would be very grateful if someone could explain to me how I can achieve this. Thanks in advance
You can use Jackson library to parse XML, it is as easy as parsing Json File. this tuto will help you to figure out.
However, bescause it is is a nested xml, you will need some nested POJOs to achieve parsing.
After a few more unsuccessful attempts, I got the idea to convert the XML into a JSON (Convert XML to JSON object in Android) and continue working with it. That worked then.
Is is possible to add new values in List Preference in for example pref_data_sync.xml from code in Android app. If yes, how to get the date from this file?
Thanks for all replies.
No, you cannot modify a xml preferences file. You cannot modify a resource android file by code.
But if you want to append it using code you can do something like this: https://stackoverflow.com/a/5437844/3482000
I am trying to parse an xml file. But i get this error:
org.w3c.dom.DOMException: Only one root element allowed
at the line:
Document doc = dBuilder.parse(getAssets().open("myfile.xml"));
Please help me solve this.
Thanks in advance.
You must have only one root, it means you must have only once a tag that surround every all tags. For example you can surround all your tags by <root></root>
Also, try to validate your xml file with an xml validator in order to see if your xml is malformed.
I looking for solution to parse xml file from res/xml folder with DOM (to read, modify xml).
The method
getResources().getXml(id)
just return XmlResourceParser object, how can I convert it into Document to parse?
I also followed this question but it does not help.
How can I solve that? Thanks in advance!
You should place the xml file in the res/raw folder, and then you can use getResources().getRawResource(R.raw.resource_id) to get an InputStream which you can process using all the standard DOM methods (wrap it in an InputSource, etc).
you can refer this link which is below
https://github.com/commonsguy/cw-android/tree/master/Resources/XML
this is the simple demo of dom parcer which is call xml file from locally.so you can try
also from webservices..try and make sure you get your data
Sorry, this seems like such an obvious question, but I just can't find an answer in the docs...
I have my own XML doc (not Android related) that I need to pull and parse at run time.
Where do I store it and what's the best way to retrieve it? Do I just read it as a text document into an XML parser? Or does Android come prepared to hand me back a parsed XML object from a well formed xml?
TIA
Put it in a xml folder in res.
You can put it in assets folder of your project.
For parsing you can use openXmlResourceParser(filename) method of AssetsManager class
XmlResourceParser parser = getResources().getAssets().openXmlResourceParser (String fileName);
Best way is to put your xml in assets folder and read xml from assets.
Read xml from assets