So, I have a file like
<root>
<transaction ts="1">
<abc><def></def></abc>
</transaction>
<transaction ts="2">
<abc><def></def></abc>
</transaction>
</root>
So, I have a condition which says if ts="2" then do something ... Now the problem is when it finds ts="1" it still scans through tags < abc>< def> and then reaches < transaction ts="2">
Is there a way when the condition doesn`t match the parsing breaks and look for the next transaction tag directly?
A SAX parser must scan thru all sub trees (like your "< abc>< def>< /def>< /abc>") to know where the next element starts. No way to get around it, which is also the reason why you cannot parallelize a XML Parser for a single XML document.
The only two ways of tuning I can think of in your case:
1) If you have many XML documents to parse, you can run one Parser for each document in its own thread. This would at least parallelize the overall work and utilize all CPU's and Cores you have available.
2) If you just need to read up to a certain condition (like you mentioned < transaction ts="2">) you can skip parsing as soon as that condition is reached. If skipping the parser would help, the way to this is by throwing an Exception.
Your implementation of startElement
within the ContentHandler
would look like this:
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if(atts == null) return;
if(localName.equals("transaction") && "2".equals(atts.getValue("ts"))) {
// TODO: Whatever should happen when condition is reached
throw new SAXException("Condition reached. Just skip rest of parsing");
}
}