2011-03-30 64 views
2

我試着去解析由谷歌地理代碼API返回的XML,但即時得到下面的錯誤在解析..錯誤而在Java解析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(); 
    } 

請建議兆瓦一些幫助在此..

感謝ü。

+0

看到[此帖](http://stackoverflow.com/questions/708327/premature-end-of-file-error-when-java-read-and-寫-xml-data-files) – 2011-03-30 05:17:52

+0

嗨,John感謝您的回覆。你建議的鏈接更多關於同步問題,如果我沒有錯。但我沒有從中得到太多的幫助.. – bHaRaTh 2011-03-30 05:25:08

+0

@ user593424:我想,它可能是缺乏關閉XML標記(S)。 – 2011-03-30 05:28:09

回答

5

您的調試代碼是問題。您閱讀該文檔顯示在這裏

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

的XML其中推進流過去所有的數據,然後嘗試用解析器讀一遍。

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

+0

嗨,MeBigFatGuy是的,你說得對,那是我非常感謝你解決的錯誤。 – bHaRaTh 2011-03-30 05:33:02

1

你可能想從你的緩衝區

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

,而不是InputStream的解析。

0

這裏是我如何解析谷歌地圖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); 

.....

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

告訴我,如果它的幫助:)