Android XML Parsing
Hi,
I'm having problems parsing a relatively large XML file on the Android platform.
The XML I'm trying to parse is about 1.5 megabytes - and while I walk through each node with a recursive function, the data is saved to a database node by node.
This is my recursive function:
function parseXMLData(xml, level)
{
var xmlElem = xml;
if (xmlElem.nodeName == 'page')
{
savePageToDatabase(xmlElem, level);
}
if (xmlElem.nodeName == 'element' && xmlElem.getAttribute('dataStructure') == 'Absatz')
{
saveContentToDatabase(xmlElem);
}
if (xmlElem.nodeName == 'element' && xmlElem.getAttribute('ctype') == 'text')
{
saveTextToDatabase(xmlElem);
}
if (xmlElem.hasChildNodes())
{
++level;
}
else
{
--level;
}
for (var i = 0; i < xmlElem.childNodes.length; ++i)
{
parseXMLData(xmlElem.childNodes.item(i), level);
}
}
Now what I get is a java.lang.OutOfMemoryError Exception that ends the parsing of the XML when I try to walk thorugh that 1.5 megabyte file. What irritates me, is that the code works fine for smaller XML files (400kb).
AND: This code works just perfectly fine for the same large file on iOs devices!
Is there any way to free some memory or has anybody got a good solution to this problem?
2 Answers
-
Peter
Are you using Titanium's parseString? Where is this code?
You are probably going to have problems parsing an XML file as large as 1.5MB for this reason, I'm afraid. The recommended approach is always to use JSON, as it incurs far less overhead. Is it possible for you to move to it?
Cheers
-
Hi Paul,
thanks for your answer.
I'm actually retrieving the XML file from a webserver via
Ti.Network.createHTTPClient
and then callparseXMLData(this.responseXML, 0);
in the onload event for the first time to start the recursion.What I might do now is to write some server side script that transforms the XML to a JSON File and then try to parse that, just for Android… as it all works fine on iOS. Might there be somewhere be a memory leak on Android?