Android Xml resource parser node parsing only - android

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

Related

How to get the inner tags from an XML file?

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.

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.

XML Parser in android

I need to know what is the best way to parsing XML file in android, I know there is 3 parser (XMLPullParser, Dom Parser and Sax parser) so whats the different between it and if there any code to do that.
Sax Parser : Simple API of XML Parse node to node, using top-down traversing, parse without storing xml, Faster compared to Dom Manipulating of node like insertion or deletion is allowed. Needs SAXParserFactory
Dom Parser : Document Object Model Stores entire xml in memory before processing, traverse in any direction, Manipulating of node like insertion or deletion is NOT allowed. Needs DocumentBuilderFactory
Pull Parser: It provides more control and speed from the above two.
Android training recommends XMLPullParser.
http://developer.android.com/training/basics/network-ops/xml.html
We recommend XmlPullParser, which is an efficient and maintainable way to parse XML on Android.
They also give some code examples.

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

Parse Xml to get attribute value in android using android SAX packages mathods

I am parsing an XML file in which certain element having attributes.
Is there any way to get the attribute value using methods given in android SAX package?
Here's my real answer. :) You probably meant android.sax.
Then you need to add a StartElementListener and in start() you'll find your Attributes.

Categories

Resources