I using SAX to parse XML & fetch number of elements from it.
In XML, there exists <item> tag number of times(easily > 50). But I want to fetch it only first 20.
Code of calling XMLHanlder:
XMLHandler handler = new XMLHandler();
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(in));
Code for XMLHandler:
public class XMLHanlder extends DefaultHandler {
@Override
public void startDocument() throws SAXException {
...
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
...
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
...
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
...
}
}
Is there any way that startElement()/endElement() does not get called more than 20 times.
If not is there any workaround?
Thanks
SAXdoes not offer any such functionality. A workaround is simply to use a counter:Optional here are the explicit assignment to
0for the counter value (will take that value by default).