在java中解析xml时出错

bHa*_*aTh 3 java xml

我试图解析由Google Geo代码API返回的XML,但是我在解析时遇到以下错误.

 [Fatal Error] :1:1: Premature end of file.
    org.xml.sax.SAXParseException: Premature end of file.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
at test2.main(test2.java:55)
Run Code Online (Sandbox Code Playgroud)

amd我的代码是这样的..我确定我正确得到响应xml ..

        URL u = new URL("https://maps.googleapis.com/maps/api/geocode/xml?latlng=12.983333,77.583333&sensor=false");  
        URLConnection uc = u.openConnection();
        uc.setDoOutput(true);

        StringBuffer sbuf=new StringBuffer();

        inxml=uc.getInputStream();

        BufferedReader in = new BufferedReader(
        new InputStreamReader(inxml));

        String res;
        //System.out.println(" Response from Google Maps "+res);
        while ((res = in.readLine()) != null){
               sbuf.append(res).append("\n");
        }
        in.close();

        System.out.println(" Total Data received  "+sbuf);

        //XML PARSE Starts Here........

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        Document doc = docBuilder.parse(inxml);

     // normalize text representation
        doc.getDocumentElement ().normalize();

       // System.out.println("Root element of the doc is "+doc.getDocumentElement().getNodeName());

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

请建议一些帮助...

感谢你.

MeB*_*Guy 5

您的调试代码是问题所在.您阅读文档以在此处显示xml

while ((res = in.readLine()) != null){
           sbuf.append(res).append("\n");
    }
Run Code Online (Sandbox Code Playgroud)

它会使流经过所有数据,然后尝试使用解析器再次读取它.

如果删除调试代码,它应该工作.