i am parsing a big xml document with sax parser, what i want to do is to stop parsing when a specific element is found and return an ArrayList which i was filling when i was parsing my file. Hwo to do ?
i have tried to create an exception but i couldn't return the list i was filling is there any other solution for my problem ?
here is my function
public List<String> parse(String id) throws ParserConfigurationException, SAXException, IOException, ClassNotFoundException
{
List<String> list_Lat_Lon = new ArrayList<String>();
//****************************************************************************************************
try{
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true); //--- or false
SAXParser saxParser = factory.newSAXParser();
DefaultHandler content_handler;
content_handler = new DefaultHandler() {
@Override
public void startDocument() throws SAXException {
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)throws SAXException
{
if(qName.equals("node")){
if(attributes.getValue("id").equals(id)){
String lat = attributes.getValue("lat");
String lon = attributes.getValue("lon");
list_Lat_Lon.add(lat);
list_Lat_Lon.add(lon); // filling my List
}
}
if(qName.equals("way")){
**// here i want to break my parsing and return my List**
}
}
@Override
public void endElement(String uri, String localName, String qName)throws SAXException
{
}
@Override
public void characters(char ch[], int start, int length)throws SAXException
{
}
@Override
public void endDocument() throws SAXException {
}
};
saxParser.parse("src/ParsingOSM/algeria-latest.osm", content_handler);
}catch (ParserConfigurationException e) {
System.out.println(e.toString());
}
return list_Lat_Lon;
}