I want to know the difference between the four above types (SAXPaser, XPath, DOM, XMLPullParse) and when should we use each one.
SAX Parsing is the Best one to implement than DOM, see the difference between these two in the following:
DOM
The Nodes are in the form of Tree Structure
Memory: It Occupies more memory, DOM is only preffered in the case of small XML documents
Slower at runtime
Stored as an objects
Programmatically easy to implement
Ease of navigation and use.
SAX
Sequence of events
It doesn't use any memory preferred for large documents.
Faster at runtime, because of the above mentioned point.
Objects are to be created.
Need to write code for creating objects
In SAX Backward navigation is not possible as it sequentially processes the document
So if you have very large files then you should use SAX parser since it will fire events and releasing them ,nothing is stored in memory ,and using SAX parser you can't access element in a random way there is no going back ! , but Dom let you access any part of the xml file since it keeps the whole file/document in memory .
see this article and you can get what you want by reading the Summary.
also check this link to view performance of different xml parser
Please check below links...
http://steveliles.github.com/comparing_methods_of_xml_parsing_in_android.html
http://xjaphx.wordpress.com/2011/11/01/android-xml-adventure-compare-xml-parsers/
http://www.ibm.com/developerworks/opensource/library/x-android/index.html
http://www.developer.com/ws/android/development-tools/Android-XML-Parser-Performance-3824221-2.htm
http://www.geekinterview.com/question_details/12797
(As Per above Article)
Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation
SAX:
Parses node by node
Doesnt store the XML in memory
We cant insert or delete a node
Top to bottom traversing
DOM
Stores the entire XML document into memory before processing
Occupies more memory
We can insert or delete nodes
Traverse in any direction.
If we need to find a node and doesnt need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.
DOM
The Nodes are in the form of Tree Structure
Memory: It Occupies more memory, DOM is only preffered in the case of small XML documents..Store the entire XML document into memory befor processing
Slower at runtime
Stored as an objects
Programmatically easy to implement
Ease of navigation and use,can traverse in any direction.
We can insert or delete,alter nodes.
SAX : use when you want to access XML ( not alter XML)
Sequence of events
It doesn't use any memory preferred for large documents.Doesn't store the XML in memory before processing
Faster at runtime, because of the above mentioned point.
Objects are to be created.
Need to write code for creating objects
In SAX Backward navigation is not possible as it sequentially processes the document,top to bottom traversing
We can't insert or delete a node
XPATH: Xpath is useful when you only need a couple of values from the XML document and you know where to find them(you know the path of the data./root/item/challange/text)
XMLPullParser:
Fast and requires less memory with DOM
Source:
http://www.time2ask.com/
http://www.time2ask.com/Android/The-difference-among-SAX-ParserXPathDOMXMLPullParser/_2361836
Related
I want to build an android app for a restaurant. I want it to display the menu in several activities using listviews. One for drinks, one for meals, one for deserts, etc. The menu comes from a json file which is being parsed in a splashscreen on app start.
Now i'm wondering what's the best way to populate the listviews and activities after parsing the json:
Populating the listviews from the splashscreen activity?
Storing the menu data in files and access them on the depending activity's oncreate()…?
Parsing a separate json file for each activity?
What is the best way in terms of performance, simplicity and effectiveness?
That would imply you have all you Acticities with their ListViews up & running when the splashscreen is starting. Probably not what you want.
You already said the JSON is stored in a file. You can parse that JSON in your splashscreen Activity, keep a global reference to it and let you Activities pick the part they need from that global class / JSONObject.
Tradeoff: Parse the complete JSON once and let all Activities retrieve the parts they need from it (this is basically 2.) at the 'risk' of parts of it never being used or split the JSON into several parts that are being loaded/parsed on demand by every Activity seperately, but having the overhead of doing file transactions in every Activity.
Either way, if the menu you're storing isn't immensely huge, the difference in performance will be minimal.
I'd go 2., load the whole file at startup, store the data globally and let every Activity make use of it.
I want to make a progress bar while importing my articles from an XML feed.
The parsing works fine, but for my progress bar I need to quickly know the total # of <item>s in the feed so I can determine percentage that have been loaded.
My thought was, it would be a lot faster to just do this in PHP and add the "count" to the feed itself - something like this:
<?xml version="1.0" encoding="utf-8"?>
<channel>
<title>My Apps Feed</title>
<link>http://link_to_this_fiel</link>
<language>en-us</language>
<count>42</count>
But then I need to be able to quickly access that "count" number.
At the moment, I have an RSSHandler.java that's being called like this:
//Add all items from the parsed XML
for(NewsItem item : parser.getParsedItems())
{
//...
Note: Min API level 8 for my app
You can use Xpath to get specific node value in XML. Sample would be:
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(.....); // get document for your xml here
Element elem = (Element) XPath.selectSingleNode(doc, "channel/count");
System.out.println(elem.getValue());
This way you can directly get count value. But I'm not sure this is the efficient and faster way to do this. As an option you can use this.
Also spend some time in reading this: SAX parsing - efficient way to get text nodes
SAX reads the tags in the order they come in. Make sure to put your tag at the beginning of the XML, otherwise you will need to parse it all anyways. For easier parsing, I'd put it into an attribute of a self-closing tag with a unique name. Then you wait until the SAX parser calls your startElement method, check if the tag name matches the name of your count tag, extract the attribute, and display it to the user.
If you want to stop the parser after displaying the count, you can throw a SAXException to do so.
I assume you already know how to do the parsing work off the main thread, as you mention a progress bar and imply that parsing can take some time (and doing long tasks on the main thread gives you ANRs). In case someone stumbles upon this question who doesn't know: You can use AsyncTask and the publishProgress (called in the "worker" thread by your code) and onProgressUpdate (called by Android on the UI thread once you call publishProgress) methods to take care of that.
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.
Usually when parsing XML i have to create all those lists with nodes, childs and what not.
Maybe there's a more simple way?...
For example I have a document that contains a few elements which id starts with "patch".
<rect id="patchW" x="48" display="none" fill="#993333" width="48" height="75"/>
And what I need is to make a list that would contain id ( of the elements that start with "patch" ), x and y ( if not present, then 0 ).
What do you think would be the best way of doing it?
Thanks!
Your best bet is SAX, which will fire events such as "start of element," "end of element," etc. Just listen to the start element events, and add the information you care about into a list every time you get an element whose ID starts with patch. Also, SAX is extremely fast - a lot of other XML parsing libraries, such as DOM, are built on top of SAX.
You should use a Parser such as JSOUP
Very simple to use and works tremendously well
you should use Xstream.It is useful.
Just wondering what would be the best way to grab the following data and parse it.
Here's an example of some the data I want to pull.
<?xml version="1.0" encoding="UTF-8" ?>
<eveapi version="2">
<currentTime>2010-11-19 19:23:44</currentTime>
<result>
<rowset name="characters" key="characterID" columns="name,characterID,corporationName,corporationID">
<row name="jennyhills" characterID="90052591" corporationName="Imperial Academy" corporationID="1000166" />
</rowset>
</result>
<cachedUntil>2010-11-19 20:20:44</cachedUntil>
</eveapi>
I've seen some examples on how to parse XML data but they are all based on if statements and that's a lot of hard coding is there a more generic way to do this?
Parsers are quite hardcoded that's the way they work. You can only check if a certain tag matches a certain pattern and then decide what to do. Especially for simple documents like yours that is absolutely no problem.
If you have more than one type of document to parse then I recommend reading this SO answer.
The "parsing", taking the term literally, is easy. Parsing is the process of taking a text string (in your case, from an http response) and turning it into a data structure such as an XML document tree. That process is handled for you by an XML parser, and you typically don't need to worry about it.
The part you're facing is how to query data from the parsed XML document, right? The easiest way to depends greatly on what you need to do with the data. But XPath is a good way to select data without a lot of verbose if statements and get-child function calls.
See also this question on using XPath in Android.