3

I’m building an android app and I have a xml file which is my data source, I load it to make queries using XPath and then load the results in a ListView. The problem comes out when the xml file start to become bigger(up to 10MB). It means that i could have problems with memory, i guess. I would like to know what’s the best way to load the xml and make searchs into the file without compromise the device’s memory or waste valuable time processing data.

Here is when i load the xml file:

InputStream isRaw = context.getResources().openRawResource(R.raw.xmlbooksource);
_xmlBookSource = new InputSource(isRaw);

And here i make a query using XPath:

nodeResultSet = (NodeList) xpath.evaluate(xpathExpression, _xmlBookSource, XPathConstants.NODESET);

all advices are welcome.

AXSM
  • 1,142
  • 1
  • 12
  • 27
  • I had the same problem (in fact I get an OutOfMemory error with an XML around 40 MiB). There's no way to solve except write your own optimized parser for your case. – m0skit0 Jan 23 '13 at 17:31
  • I had to take another approach and i've been using a SAX parser. to read the xml, Although the file which i'd tried is very small. so i'm going to make other tests with larger files. – AXSM Feb 04 '13 at 15:41
  • I'm using SAX. It does not work over over around 40 MiB. If you don't want to waste your time like I did: none of Android's XML parsers works. – m0skit0 Feb 04 '13 at 15:48
  • Annnnd what do you did? – AXSM Feb 04 '13 at 16:30
  • Annnnnnnnd as I said, you either write your own XML parser to get around those limitations, or do not use such big XMLs. – m0skit0 Feb 04 '13 at 16:40

1 Answers1

1

While they can be used as a data source and can be searched using XPath; XML are not really made for these purposes and should be limited to the transfert of data between systems.

If your data source is anything but very small, you should seriously think about using a database like SQLite for storing and querying your data.

SylvainL
  • 3,926
  • 3
  • 20
  • 24
  • The reason which i've chosen an xml file as my DS because i think the xml file is easier as make to changes as SQlite. in SQlite you're tied to a structure. – AXSM Feb 04 '13 at 15:25
  • your advice made me think twice and going back to my design. but now a got some new issues, so if you know something about you are welcome. this is the [link](http://stackoverflow.com/questions/14760507/receiving-and-sending-messages-over-the-network-from-a-background-process) – AXSM Feb 07 '13 at 20:50