如何修复从builder.parse(xmlString)返回的SAXException?

时间:2013-10-23 00:18:35

标签: java xml dom

我在使用DocumentBuilderFactory解析xml字符串时遇到问题。当我查看log.d(“文件”,文件)时,文件包含我想要的xml字符串。但是,当我使用Document doc = db.parse(inputSource)来获取doc时,它会返回一个SAXException。有人能告诉我编码有什么问题吗?谢谢。

        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");
    }

2 个答案:

答案 0 :(得分:1)

我猜这是因为你正在把它读成一个java字符串,它正在逃避你正在回读为字符流的一些字符。

这似乎对我有用:

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

答案 1 :(得分:0)

我从错误消息中可以理解,它正在寻找</leg>,因为它显示expected: /leg并且它有start_location

您可能错过了某个地方的<leg>标记