Parse a local xml file and show parsed data in web-view - android

I have an xml file which is stored locally in my app and I want to show parsed data from it in web-view.

We need more information regarding what exactly you want to do. It is very unclear. There are two things that you could mean:
The XML file is an XHMTL file, that you want to render and show in a webview.
You want to apply styling to an XML file and display it in the webview, preferably using XSLT.
In the former case, it is pretty straightforward. If it is the latter, check out this answer: How can I transform xml to html on android?

To access XML resources stored in res/xml, call getResources().getXml() from any Activity or other Context. You need to supply to getXml() the ID of the XML to load (R.xml.myfile).
to read your xml add code as shown below
XmlResourceParser myxml = mContext.getResources().getXml(R.xml.MyXml);
//MyXml.xml is name of our xml in newly created xml folder, mContext is the current context
// Alternatively use: XmlResourceParser myxml = getContext().getResources().getXml(R.xml.MyXml);
myxml.next();//Get next parse event
int eventType = myxml.getEventType(); //Get current xml event i.e., START_DOCUMENT etc.
and to get content of code add code shown below

Related

External data in Android

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.

Android Xml resource parser node parsing only

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 :)

Update a XML file

I have a file that I open and parse and some of the categories can be modified. Im wondering how I can update the original Xml file with the new values.
I assume that the XML file is a normal sequential file. If so, you could try to update just one tag, but it would be easier and safer just to rewrite the entire file.

Where do I put (and how do I read) my own custom XML in Android?

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

Load file from resource

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

Categories

Resources