Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中解析xml时出错_Java_Xml - Fatal编程技术网

在java中解析xml时出错

在java中解析xml时出错,java,xml,Java,Xml,我试图解析Google Geo code 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.xerce

我试图解析Google Geo code 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)
我的代码是这样的。。我确信我正确地得到了响应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();
    }
请在这方面给我一些帮助


谢谢您。

您可能需要从缓冲区进行解析

Document doc = docBuilder.parse(new InputSource(new StringReader(sbuf.toString())));

而不是inputstream。

问题出在调试代码上。您阅读文档以在此处显示xml

while ((res = in.readLine()) != null){
           sbuf.append(res).append("\n");
    }
这会使流超过所有数据,然后您尝试使用解析器再次读取它


如果删除调试代码,它应该可以工作。

以下是我如何解析Google Maps API的示例:

        String input = URLEncoder.encode(input, "UTF-8");
        String addressToConnect = "https://maps.googleapis.com/maps/api/place/autocomplete/xml?input="+input+"&types=geocode&language=fr&sensor=true&key="+APIKey;

        //Partie connexion
        URL url = new URL(addressToConnect);
        HttpURLConnection connexion = (HttpURLConnection) url.openConnection();
        connexion.connect();
        InputSource geocoderResultInputSource;
        geocoderResultInputSource = new InputSource(connexion.getInputStream());

        //Partie XML/XPATH
        Document geocoderResultDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(geocoderResultInputSource);
        XPath xpath = XPathFactory.newInstance().newXPath();

        //On s'assure que la requete envoyée à l'API à bien renvoyée un STATUS = OK cf. Google API
        NodeList nodeListCodeResult = (NodeList) xpath.evaluate("//status", geocoderResultDocument, XPathConstants.NODESET);

你可以举一个完整的例子。我已经开始用一些使用这种机制的方法开发这个库


如果有帮助,请告诉我:)

见你好,约翰,谢谢你的回复。u建议的链接更多的是关于同步问题,如果我没有错的话。但是我没有从中得到太多帮助。@user593424:我想,可能是缺少结束XML标记。嗨,约翰,谢谢你的帮助。。MeBigFatGuy解决了我的问题。嗨,MeBigFatGuy是的,你是对的,这是我犯的错误。非常感谢你的解决。。