Im trying to parse the XML returned by the Google Geo code API ,but im getting the following error while parsing..
[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 my code is like this.. im sure that im getting the response xml correctly..
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();
}
please suggest mw some help on this..
Thank u.
Your debugging code is the problem. You read the document to show the xml here
which advances the stream past all the data, then you try to read it again with the parser.
If you remove the debug code it should work.