views:

46

answers:

2

Hello everybody,

I have a problem with the libxml parser on the iPhone! Im using the PerformXMLXPathQuery to parse a xml file with about 100'000 lines. The app breaks while running the PerformXMLPathQuery because it runs out of memory. What can I do here?

Thanks,

Markus

+1  A: 

You can try to use the NSXMLParser to get only data you want to save memory usage

Benoît
We first had NSXMLParser but this parser is way to slow for such an amount of data...
Markus
are u sure that the problem is a problem of memory ?
Benoît
I think so, If I run it in Instruments, the Live Bytes allocation goes up rapidly to 28.61 MB and then breaks suddenly! When look at the Device Log, the type of report is: Low Memory
Markus
Could it be, that a special character in the xml file cause a uncontrolled Memory increase?
Markus
+3  A: 

You may want to look at libxml2's xmlTextReader "streaming" interface.

It's a foward-moving cursor that discards input data it has already processed, and doesn't build a large, memory intensive DOM as it moves forward. XPath queries can still be used, with the limitation that subtrees must be expanded (i.e., copied into in-memory libxml structures) for parts of the document that are to be searched.

The xmlTextReader interface is much more tedious than just throwing a few XPath queries at the root element, but for a document that size, on a device that's memory constrained, it may be your best bet.

I've just finished a conversion to xmlTextReader for similar reasons, and the memory used during XML processing is identical for a 20KB and 30MB document.

Mieko