I'm following this tutorial.
It works great but I would like it to return an array with all the strings instead of a single string with the last element.
Any ideas how to do this?
I'm following this tutorial.
It works great but I would like it to return an array with all the strings instead of a single string with the last element.
Any ideas how to do this?
In many problems it is necessary to use different kinds of xml files for different purposes. I will not attempt to grasp the immensity and tell from my own experience what I needed all this.
Java, perhaps, my favorite programming language. In addition, this love is strengthened by the fact that you can solve any problem and come up with a bike is not necessary.
So, it took me to create a bunch of client-server running a database that would allow the client to remotely make entries in the database server. Needless to be checking input data, etc. and the like, but it's not about that.
As a principle of work, I, without hesitation, chose the transmission of information in the form of xml file. Of the following types:
Make it easier to read any further, except to say that it is the information about doctors institutions. Last name, first name, unique id, and so on. In general, the data series. This file safely got on the server side, and then start parsing the file.
Of the two options parsing (SAX vs DOM) I chose SAX view of the fact that he works more bright, and he was the first I fell into the hands :)
So. As you know, to work successfully with the parser, we need to override the needed methods DefaultHandler's. To begin, connect the required packages.
Now we can start writing our parser
Let's start with the method startDocument (). He, as the name implies, reacts to an event beginning of the document. Here you can hang a variety of actions such as memory allocation, or to reset the values, but our example is pretty simple, so just mark the beginning of work of an appropriate message:
Next. The parser goes through the document meets the element of its structure. Starts method startElement (). And in fact, his appearance this: startElement (String namespaceURI, String localName, String qName, Attributes atts). Here namespaceURI - the namespace, localName - the local name of the element, qName- a combination of local name with a namespace (separated by a colon) and atts - the attributes of this element. In this case, all simple. It suffices to use qName'om and throw it into some service line thisElement. Thus we mark in which the element at the moment we are.
Next, meeting item we get to its meaning. Here include methods characters (). He has the form: characters (char [] ch, int start, int length). Well here everything is clear. ch - a file containing the string itself self-importance within this element. start and length - the number of service indicating the starting point in the line and length.
Ah, yes. I almost forgot. As the object of which will be to fold naparsennye data speaks to the type of Doctors. This class is defined and has all the necessary setters-getters.
Next obvious element ends and it is followed by the next. Responsible for ending the endElement (). It signals to us that the item has ended and you can do anything at this time. Will proceed. Cleanse Element.
Coming so the entire document, we come to the end of the file. Work endDocument (). In it, we can free up memory, do some diagnostichesuyu printing, etc. In our case, just write about what parsing ends.
So we got a class to parse xml our format. Here is the full text:
I hope the topic helped to easily present the essence of the SAX parser.
Do not judge strictly first article :) I hope it was at least someone useful.
UPD: To run this parser, you can use this code:
So you want to build a XML parser to parse a RSS feed like this one.
Now you have two SAX implementations you can work with. Either you use the
org.xml.sax
or theandroid.sax
implementation. I'm going to explain the pro's and con's of both after posting a short hander example.android.sax Implementation
Let's start with the
android.sax
implementation.You have first have to define the XML structure using the
RootElement
andElement
objects.In any case I would work with POJOs (Plain Old Java Objects) which would hold your data. Here would be the POJOs needed.
Channel.java
This class implements the
Serializable
interface so you can put it into aBundle
and do something with it.Now we need a class to hold our items. In this case I'm just going to extend the
ArrayList
class.Items.java
Thats it for our items container. We now need a class to hold the data of every single item.
Item.java
Example:
Now that was a very quick example as you can see. The major advantage of using the
android.sax
SAX implementation is that you can define the structure of the XML you have to parse and then just add an event listener to the appropriate elements. The disadvantage is that the code get quite repeating and bloated.org.xml.sax Implementation
The
org.xml.sax
SAX handler implementation is a bit different.Here you don't specify or declare you XML structure but just listening for events. The most widely used ones are following events:
An example handler implementation using the Channel object above looks like this.
Example
Now to be honest I can't really tell you any real advantage of this handler implementation over the
android.sax
one. I can however tell you the disadvantage which should be pretty obvious by now. Take a look at the else if statement in thestartElement
method. Due to the fact that we have the tags<title>
,link
anddescription
we have to track there in the XML structure we are at the moment. That is if we encounter a<item>
starting tag we set theinItem
flag totrue
to ensure that we map the correct data to the correct object and in theendElement
method we set that flag tofalse
if we encounter a</item>
tag. To signalize that we are done with that item tag.In this example it is pretty easy to manage that but having to parse a more complex structure with repeating tags in different levels becomes tricky. There you'd have to either use Enums for example to set your current state and a lot of switch/case statemenets to check where you are or a more elegant solution would be some kind of tag tracker using a tag stack.