DOM xml parsing from file res/xml in Android - android

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

Related

Read XML from R.xml and not as an XMLPullParser

I want to pass my xmls from my R.xml directory to Simple library for deserialization.
The library accepts Readers and InputSreams and such.
But from R.xml I get XMLPullParser. How can I get something like a Reader or InputStream instead?
if you save your xml fine inside res/raw instead of res/xml, you should be able to use openRawResource
getResources().openRawResource(R.raw.xml_name)
which returns an InputStream

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

How to Pull an RSS Feed using XmlPullParser in Android

I'm building an RSS reader APP, and I've been told to use the XMLPullParser interface.
Here is the block of code I'm working with:
XmlResourceParser parser = context.getResources().getXml(resource);
'Resource' is a an integer with the R.id. integer of the Xml file. This is not an internal XML file, so I don't know how to work around this.
Any ideas? Is the XmlResourceParser the wrong approach for this project? I've seen XMLReaders used with content handlers as well. Can you integrate these technologies together?
Thank you
what is the type of your xml source?
xmlPullParser can be used to parse any xml sources.
It is in my opinion the way to do this. Only problem you may encounter is when the rss feed has empty lines. The xml pullparser of android (api-level 14) jumps to the /channel if this is the case. When implementing the parser try to use the AsyncTask to start the reading of the rss feed.
Success with the implementation.

where to store XML files

I'm storing app data in a few XML files. Should I put these XML files in the 'res' folder? Is there anything special I need to do besides just using something like:
XmlPullParser xpp = getResources().getXml(R.xml.myXMLfile);
Thanks
I would suggest to store these file in the assets folder. Use the following code to read file form the assets folder:
getResources().getAssets().open(fileName);
No, nothing, as long as these are completely static in nature.
If it is an app data & if you feel to have lightweight app then try to store in the internal memory ,make use of it & get benefited.

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

Categories

Resources