我試着去解析由谷歌地理代碼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();
}
請建議兆瓦一些幫助在此..
感謝ü。
看到[此帖](http://stackoverflow.com/questions/708327/premature-end-of-file-error-when-java-read-and-寫-xml-data-files) – 2011-03-30 05:17:52
嗨,John感謝您的回覆。你建議的鏈接更多關於同步問題,如果我沒有錯。但我沒有從中得到太多的幫助.. – bHaRaTh 2011-03-30 05:25:08
@ user593424:我想,它可能是缺乏關閉XML標記(S)。 – 2011-03-30 05:28:09