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

标签 java xml dom

我在使用 DocumentBuilderFactory 解析 xml 字符串时遇到问题。当我查看 log.d("File",file) 时,文件包含我想要的 xml 字符串。但是,当我使用 Document doc = db.parse(inputSource) 获取文档时,它返回 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");
    }

最佳答案

我猜测是因为您正在将其读入 java 字符串,该字符串转义了您作为字符流读回的一些字符。

这似乎对我有用:

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

关于java - 如何修复从 builder.parse(xmlString) 返回的 SAXException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19530610/

相关文章:

java - 在 Eclipse 中运行时从 pom 获取 maven 项目版本和工件 ID

java - 在任何派生类的构造函数之后运行方法

java - 为什么我的应用程序会抛出空指针异常?

javascript - 在开发人员控制台打开之前元素不可见

javascript - 预加载图像的最佳方式

Java并发: CopyOnWriteArrayList behavior

java - 我将如何使用 slf4j/log4j/java.util.logging robuSTLy 记录二进制或 XML?

html - 最后一个<a>元素的XPath表达式,其“href”属性以数字开头?

javascript - 获取点击元素之前的最后一个 h1 标签

java - 接口(interface)之间的转换