-2

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; 
}
  • You might consider using [StAX](https://docs.oracle.com/javase/tutorial/jaxp/stax/why.html) – Johannes Kuhn Mar 25 '18 at 19:23
  • Possible duplicate of [How to stop parsing xml document with SAX at any time?](https://stackoverflow.com/questions/1345293/how-to-stop-parsing-xml-document-with-sax-at-any-time) – Johannes Kuhn Mar 25 '18 at 19:44
  • @Johannes Kuhn, here he needs the list as a return value along with exiting the parsing. The link to duplicate question doesn't say anything about returning any object. You might wanna read my edited answer. – mark42inbound Mar 26 '18 at 14:56
  • Subclass SAXException, add a field that holds the return value, catch the subclass, extract the value. – Johannes Kuhn Mar 27 '18 at 12:02

1 Answers1

-2

You can put use a LinkedHashMap (your order of insertion will be preserved while using keys for lat and lon). As you want to return on the first occurrence. I presume it has to do something with the order of the occurrence of the elements. Put the element values in the map and operate on it after the parsing has finished.

EDIT

You said that you've tried throwing an Exception when your conditions satisfies. When you throw an Exception like throw new SAXException( ), the list of values can't be returned. A dirty trick might be, appending your string values to the exception message and retrive in the catch block. Infact, it's not a dirty solution as the exception will only be thrown when the if condition matches. And that's what you want. Also, you might think of using a custom exception, for you can catch it precisely. Hope this solves your problem.

mark42inbound
  • 364
  • 1
  • 4
  • 19
  • Nope, you can exit the parsing. – Johannes Kuhn Mar 25 '18 at 19:40
  • thank you for your answer, i understand the fact that i cannot force SAX to return based on some condition, but now i want to stop parsing when the condition is true and operate on my map after breaking my parsing because the file is too big – Tarek Abdellaziz Mar 25 '18 at 23:00
  • @TarekAbdellaziz Mark has since updated their answer. Does this solve your issue? – Rob Apr 02 '18 at 06:37
  • @TarekAbdellaziz, does my answer solve your problem? If yes, you accept it as an answer or provide the reason otherwise. – mark42inbound Apr 05 '18 at 05:51