簡體   English   中英

在java中解析xml時出錯

[英]Error while parsing xml in java

我試圖解析由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)

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

請建議一些幫助...

感謝你。

您的調試代碼是問題所在。 您閱讀文檔以在此處顯示xml

while ((res = in.readLine()) != null){
           sbuf.append(res).append("\n");
    }

它會使流經過所有數據,然后嘗試使用解析器再次讀取它。

如果刪除調試代碼,它應該工作。

您可能想要從緩沖區解析

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

而不是輸入流。

以下是我解析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);

.....

你可以在這里找到一個完整的例子。 我已經開始用一些使用這種機制的方法來開發這個庫。

告訴我它是否有幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM