What would be the easiest way to parse an rss feed? Are there any already done easy(fast to implement) methods out there?
All the posts I could find on the topic were many years old. Any new technologies out there that are worthy of attention? Or should I just make my own parser?
Edit: please link to a usage method/tutorial too :)
For bigger XML i would recomend a serial XML parser like SAX - it's build in: http://developer.android.com/reference/android/sax/package-summary.html
For examples just search in Google - there are milions out there: http://www.google.at/search?q=sax+android+tutorial
For just reading it out, you can use DOM parser, which is very simple to use and to understand.
And: It is part of the android API
You can take the source code from Android XML Parser Performance as an example. I stick to SAXParser though XmlPullParser is good as well.
DOM is not the best option for a memory-constrained device (unless you want to manipulate the tree which you don't).
Related
I would like to know which is more efficient to get the data from the server by the xml or json.
Another question:
does XmlPullParser related to parsing xml data that come from the web service? so if I am using json I don't need XmlPullParser ! or there is other uses !
thank you very much
What I've found extremely useful for parsing JSON is Google's gson library. For xml, you can use gson underneath to do the same thing with gson-xml. With a single line of code you can map your JSON/XML to your objects without having to write a single line of parsing code.
If you find performance to be an issue (I'm making this suggestion because these libs make you super productive), there are mechanisms in both to allow you finer grained control. I doubt you'll have problems with performance though.
For a very thoroughly researched answer to the headline question (though focussed on browsers, not android apps), see David Lee's Balisage 2013 paper:
http://www.balisage.net/Proceedings/vol10/html/Lee01/BalisageVol10-Lee01.html
His conclusion, in one line, is that the choice between XML and JSON makes very little difference in itself - though the details of how you do XML or how you do JSON can make a big difference.
Which all parsers are used in Android for XML parsing? Right now I know only SAX, XMLPullParser and DOM parsers. It will be really great if someone can tell the efficiency comparison for parsers used.
Thanks,
Stone
this guy has talked exactly for what you have asked.
http://www.ibm.com/developerworks/opensource/library/x-android/
though about efficiency you can find it while he is closing
SAX:
1. Parses node by node
2. Doesnt store the XML in memory
3. We cant insert or delete a node
4. Top to bottom traversing
DOM
1. Stores the entire XML document into memory before processing
2. Occupies more memory
3. We can insert or delete nodes
4. Traverse in any direction.
check this:
SAX parser vs XMLPull parser
http://www.developer.com/ws/article.php/3824221/Android-XML-Parser-Performance.htm
http://www.differencebetween.net/technology/difference-between-sax-and-dom/
Another XML Parser that I've found very useful is the Simple Java XML Parser (SJXP) available at http://www.thebuzzmedia.com/software/simple-java-xml-parser-sjxp/. It uses the XPP3 pull parser, and aims for efficiency while still being very simple to use.
The source code is also available, with great inline commenting if you want to see how it works.
when it comes to pulling XML data from a website or file what is the fastest and best way? i know there is SAX and such but before i start building an application i wanna use the most ideal way and learn on that.
It's important to understand the difference between SAX and DOM. It's explained here.
My application shall parse XML received via HTTP. As far as I understand there are three major ways of parsing XML:
SAX
DOM
XmlPullParser
It is said that SAX is the fastest of these while DOM is not optimal for larger XML documents. But what is a large XML document in terms of parsing? What would be a recommended parser for the following?
XML document size between 1-5 kB
Easy traversing through the document, i.e. I need to know not only the current element but also the parent elements.
As far as I understand there are three major ways of parsing XML:
- SAX
- DOM
- XmlPullParser
Wrong! Neither of those is the best way. What you really want is annotation based parsing using the Simple XML Framework. To see why follow this logic:
Java works with objects.
XML can be represented using Java objects. (see JAXB)
Annotations could be used to map that XML to your Java objects and vice versa.
The Simple XML Framework uses Annotations to allow you to map your Java and XML together.
Simple XML is capable of running on Android (unlike JAXB).
You should use Simple XML for all of your XML needs on Android.
And to help you do exactly that I will point you to my own blog post that explains exactly how to use the Simple library on Android.
Unless you have a 100MB XML file then Simple will be more than fast enough for you. It is for me, I use it on all of my Android XML projects.
N.B. I should point out that if you require the user to download XML files that are more than 1MB on Android then you may want to rethink your strategy. You might be doing it wrong.
I'm afraid this is a case of, it depends ...
As a rule of thumb, using Java to build a DOM tree from an XML document will consume between 4 and 10 times that document's native size (assuming Western text and UTF-8 encoding), depending on the underlying implementation. So if speed and memory-use are not critical it will not be a problem for the small documents you mention.
DOM is generally regarded as quite an unpleasant way to work with XML. For background you might want to look at Elliotte Rusty Harold's presentation: What's Wrong with XML APIs (and how to fix them).
However, using SAX can be even more tedious as the document is processed one item at a time. SAX however is fast and consumes very little memory. If you can find a pull parser you like then by all means try that.
Another approach (not super-efficient, but clean and maintainable) is to build an in-memory tree of your XML (using DOM, say) and then use XPath expressions to select the information you are interested in.
I want to show feeds from a blog in a listview. It would be really help if you guys can suggest some tutorials or how to do it?
thanks in adv.
I can point you in the right direction, using standard Android SDK components.
Your solution will consist of several pieces - an HTTP downloader, an RSS parser (presumably the blogs have public RSS feeds), a ListAdaptor, and your ListView.
Firstly, you need to grab the RSS file from the blog. There are a bunch of ways to do this, I'd suggest using HttpClient and HttpGet.
Next, you'll need to parse the RSS file you downloaded. For this, you can use XMLReader. Writing a good RSS parser is probably the trickiest bit!
Now you've got your data parsed, store it in a list and write a ListAdaptor.
Hook the ListAdaptor upto your ListView using setAdaptor and you're good to go.
If this all sounds a bit complicated, there are various Java RSS libraries that'll perform steps 1 and 2 for you.
You may also want to take a look at the source code of android-rss, and give IBM's XML article a good read!