I'm using the Android SAX parser to search for entries in a rather large (6MB) XML file. I'm basically using a deriva开发者_如何学编程tive of the code shown in listing 8 here. The question I have is how do I stop parsing once my match has been found? The code shown continues parsing through the end of the file but I want to stop before then. Is this possible or do I need to use something other than SAX (e.g. XmlPullParser?)
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) {
return true;
}
That's awful but it works...
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!!!
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.
精彩评论