Reputation: 15059
There is a remote xml file and I need to get set of xml nodes from it and show in an activity. One way is to download it completely and then do an xpath evaluate and get node set as follows
// using node I can drill further down to collect my relevant info
NodeList names = (NodeList) xPath.evaluate("/library/artists/name", new InputSource(new FileReader(getDowloadedXMLFileLocation())), XPathConstants.NODESET);
The other way is to use a pull parser which would be a bit more code I guess to do a simple job to extract a nodeset . I am wondering which method performs better in memory and speed ?
Upvotes: 0
Views: 345
Reputation: 29436
Customized code versus a generic solution, equates to high performance versus ease of use.
XPath
(and XLST
, and JDOM
etc) will construct a DOM like model, and iterate it. This will take more RAM and CPU. XPath is easy, but can be really slow for large files. Also it requires you to have the complete file at hand. Use this for small files.
PullParser
on other hand is like reading a file only at places you are interested, skipping the rest. Also, only one node is loaded in RAM at a time. There are no limits on how large the input file is, or is it fully available or being streamed over a medium. Use this for large or streamed files.
Upvotes: 1
Reputation: 26034
An excellent answer I found at Best practices for parsing XML
You can also use XmlPullParser to parse xml. But I suggest if possible, use json instead of XML, which decrease your memory and Google provide its wrapper mechanism in GSON which is best at all.
Links.
1) http://vtd-xml.sourceforge.net/
Upvotes: 1