Most efficient way to modify xml - android

I'm writing an app that needs to modify xml documents. More precisely it need to add attributes and add/delete nodes. These documents are relatively small, 30-50K at most. From what I'm reading the best way to read XML is use the SAX parser, but does that apply to modifying XML as well? The DOM parser seems to be the easiest to manipulate XML with, but, obviously, uses more battery and memory.
Just looking for the most efficient way (uses the least battery/memory) to manipulate XML on an Android device.

I suggest XOM - It is very memory efficient and support dual streaming/tree-based API.

The most efficient way to modify XML is to use VTD-XML(http://vtd-xml.sf.net)...it is the only way to do so called incremental update..

Related

App data - String array or XML : performance and memory

I am making Android app, I have to use multidimensional data, either in String [][] or in Xml resource file. Please suggest which one is better from performance perspective and memory used in both cases.
As Jack stated, the operations on a String[][] would probably have better performance than working with an XML file. It would also likely have a smaller memory footprint. Depending on what sort of data you are dealing with, a String[][] might be cumbersome to deal with. If the data is hierarchical, I would go with an XML file for simplicity.
If you thing an XML file is the way to go, take a look at this article on parsing XML in Android. The article claims that an XmlPullParser is "efficient."
You may want to consider using a JSON format as third option :)
If you only care about the performance and memory footprint, String[][] would be better.
But place your data into xml and access them as resource has its advantages.
Following is quoted from Android developer's document.
Using app resources makes it easy to update various characteristics of your app without modifying code and—by providing sets of alternative resources—enables you to optimize your app for a variety of device configurations (such as different languages and screen sizes).
In addition, by zip aligning the resources, the resource can be mapped to memory which makes the speed to access them also very fast.

To provide backup facility in my android application which format is efficient XML or CSV

I want to give backup facility in my android application. So for that purpose i don't know which which format will be suitable. I am thinking either XML or CSV. Please tell me which is efficient.
It's my opinion that you're probably better off using JSON, as it has many great advantages as listed below and given your data, wont be considerably larger than CSV or Binary.
Take a look at this post for details on how to implement it:
How to parse JSON in Android
The following is a general breakdown of the different data format options:
XML
This format is the least efficient (file size and time to parse), but comes with the advantage that it can be easily debugged or modified/read by a person. In general, use this if you are going to be reading the content, displaying it in some other program or the file will be small enough that it's size and processing disadvantages don't have a significant effect.
JSON
More concise than XML, while still maintaining it's human readability. It's syntax isn't quite as simple as XML but it's still very simple. I would recommend this over XML.
CSV
This format is much more efficient than XML, but is prone to errors if modified manually and can be very hard to read. You will likely require special care in dealing with the delimiting character so you'll want to find yourself a simple CSV library. It's disadvantages are that although
Binary
These formats will be read/written to a file as bytes. They are structured in such a way that only your specific application/reader will know how how the bytes are structured. This format is the most concise and has the best read/write performance, but of course, it's practically impossible to modify or read.
Edit: Also worth considering is your ability to modify the format of the data, for the purposes of supporting future version changes. Using JSON or XML allows you to easily add new fields or ignore existing ones and so can be easier to maintain backwards compatibility for existing applications without breaking their functionality. A similar solution for CSV or Binary would require that you store and check the data format version number with the files, and then manually switch between loaders in code.
I'd go (and I use them in my apps) for CSV files, since the data are crude and concise (i.e.: small file size and fast to read/write).
I won't choose XML files, which put a lot of garbage in the file, bloating them ridicolously.

What should I be using for an XML parser in my Android App?

I want to create an App that uses a potentially large xml file. It will also modify and ideally be able to traverse in reverse.
I know there is SAX, DOM, and the XML pull parser. The pull parser is out, unless I spend memory on creating my own tree of objects which does not seem feasible.
That leaves SAX and DOM unless there is another parser out there that can do what I want. Highly improbable, I know.
Yes, I saw this answer: https://stackoverflow.com/questions/7498616/which-xml-parser-should-i-use-for-android
Thoughts on having tree like usability without having to use DOM?
There are a lot of options when it comes to parsing XML. But it depends on your own requirements that which parser you can use when. For that you need to know the basic differences between the parser. Here is some basic information i have provided.
SAX parser is one where your code is notified as the parser walks through the XML tree,
and you are responsible for keeping track of state and constructing any objects you might want to keep track of the data as the parser marches through.
DOM parser reads the entire document and builds up an in-memory representation that you can query for different elements. Often, you can even construct XPath queries to pull out particular pieces.
And as you said you are having large file and also if you want faster performance i suggest that you should use StAX parser. Here is link for that.
Hope this will help you...
Also refer this link.
DOM is better for most of the cases where it will load all the XML at a time. But If the XML size is very big then we should go for SAX parser where it will read for the tag from the start of the XML every time.
If the XML is really big then it is better to filter from the server end by sending the requirements in the request or else we can go for pagination which is suggestible.

is there some built in resources for letting the user update an xml file in Android?

I want to keep some information in a xml file, and I want to let the user update that file. Later I will parse and use that information in my app.
Before rolling my own code to create the UI to let the user do this, I was wondering if Android already has something along the lines I could use?
Android doesn't provide XML generation code itself, but there are plenty of Java resources for it, such as JAXP.
Take a look at using the DOM parser library, that Android has as standard. There are a number of tutorials for this online. For general parsing you might tend to use the SAX parser library due to the fact that it has lower memory requirements, but the DOM parser library appears to contain all of the standard methods that you would use to modify the DOM structure once it's in memory. Just as an example, the Node class has an appendChild() method.
Once you've modified the DOM in memory, hopefully there is some way of persisting the modified Document object for later use (e.g. persist to file), though I have no first-hand experience of doing that.

Best practices for parsing XML

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.

Categories

Resources