Skip to content Skip to sidebar Skip to footer

Exiting From Xml.parse When Match Is Found

I'm using the Android SAX parser to search for entries in a rather large (6MB) XML file. I'm basically using a derivative of the code shown in listing 8 here. The question I have i

Solution 1:

Generate an ArithmeticException and catch it ! if (condition) { int a=1; a/=0; } ...

try {
    Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
} 
catch (ArithmeticException e) {
    returntrue;
}

That's awful but it works...

Solution 2:

Well DiskCrasher, Every Sax parser will work till the end generally, But if you still want to quit it while its in the middle of the work, you can always check condition and use the return statement.

But Far better way of selective parsing would be to use the XML PullParser, Coz this parser will in general work on the fly and give you data without loading the whole file. This way you can check condition and exit the loop anytime without even need to use large memory chunks...

Hope this is somehow helpful to you!!!

Solution 3:

You can throw an exception when you get a match. The exception can capture the match info and will be propagated to the original parse() caller. Catch the exception and test whether it is one based on getting a match or a true parsing error and process accordingly.

Post a Comment for "Exiting From Xml.parse When Match Is Found"