user1874435
user1874435

Reputation: 161

How to fix SAXException return from builder.parse(xmlString)?

I am having a problem parsing the xml string using DocumentBuilderFactory. When I look at the log.d("File",file), file contains the xml string as I want it to be. However, when I use Document doc = db.parse(inputSource) to get the doc, it returns an SAXException. Can someone tell me what is wrong with my coding? thank you.

        String query="https://maps.googleapis.com/maps/api/directions/xml?origin=33.78917,-118.107626&destination=33.77319,-118.125221&sensor=true";
        url = new URL(query);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection)connection;
        int responseCode = httpConnection.getResponseCode();
        InputStreamReader in = new InputStreamReader(httpConnection.getInputStream());
        InputStream inputStream = httpConnection.getInputStream();
        BufferedReader buf = new BufferedReader(in);

        String file="";         
        while(buf.readLine()!=null){                        

            file+=buf.readLine();
            Log.d("File", file);
        }                               

        if(responseCode==HttpURLConnection.HTTP_OK){

            DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
            DocumentBuilder db= dbf.newDocumentBuilder();
            //parse xml file
                //Document doc = db.parse(file);
            InputSource inputSource = new InputSource();
            inputSource.setCharacterStream(new StringReader(file));
            Log.d("COde",httpConnection.getResponseCode()+"");
            try{
                Document doc = db.parse(inputSource);               
                Element el = doc.getDocumentElement();
                NodeList nl = el.getElementsByTagName("step");
                Log.d("COde",httpConnection.getResponseCode()+"");
                Path p;
                if(nl!=null && nl.getLength()>0){
                    for(int i=0; i<nl.getLength();i++){                     
                        Node entry =nl.item(i);
                        NodeList child= entry.getChildNodes();
                        Log.d("List Size",list.size()+"size");
                        Element elements = (Element)entry.getChildNodes();
                        Element sPoint = (Element) elements.getElementsByTagName("start_location");
                        Element ePoint = (Element)elements.getElementsByTagName("end_location");                                                
                        Point sP = new Point(sPoint.getFirstChild().getNodeValue(),sPoint.getLastChild().getTextContent());
                        Point eP = new Point(ePoint.getFirstChild().getTextContent(),ePoint.getLastChild().getTextContent());
                        String durationValue = elements.getElementsByTagName("duration").item(0).getNodeValue();
                        Log.d("Duration",durationValue);
                        String durationText = elements.getElementsByTagName("duration").item(1).getNodeValue();
                        String instr= elements.getElementsByTagName("html_instructions").item(0).getNodeValue();
                        String distanceValue =elements.getElementsByTagName("distance").item(0).getNodeValue();
                        String distanceText = elements.getElementsByTagName("duration").item(1).getNodeValue();
                        p = new Path(sP,eP,durationText,distanceText,instr);                        
                        list.add(p);
                        Log.d("List Size",list.size()+"size");
                    }
                }
            }
            finally{}
        }           
    }

    catch(MalformedURLException e1){
        Log.d("HTTP_CALL","MalformedURLException");
    }                   
    catch(ParserConfigurationException e1){
        Log.d("HTTP_CALL","ParserConfigurationException");
    }       
    catch(SAXException e1){
        Log.d("HTTP_CALL","SAXException");
    }catch(IOException e1){
        Log.d("HTTP_CALL","IOException");
    }

Upvotes: 0

Views: 235

Answers (2)

An SO User
An SO User

Reputation: 25028

What I can understand from the error message, it was looking for </leg> because it says expected: /leg and it got a start_location.

You may have missed closing the <leg> tag somewhere

Upvotes: 0

Craig
Craig

Reputation: 1390

I'm guessing that because you are reading it into a java string which is escaping some of the characters which you are reading back as a Character Stream.

This seemed to work for me:

InputStream inputStream = connection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(inputStream);

Upvotes: 1

Related Questions