3

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.

4
  • see this post Commented Mar 30, 2011 at 5:17
  • hi John thanks for the reply. the link u suggested is more regarding the synchronization issue,if im not wrong. but i didnt got much help from that.. Commented Mar 30, 2011 at 5:25
  • @user593424: I guess, it might be lack of closing XML tag(s). Commented Mar 30, 2011 at 5:28
  • hi John , Thanks for the help..MeBigFatGuy solved my problem. Commented Mar 30, 2011 at 5:34

3 Answers 3

5

Your debugging code is the problem. You read the document to show the xml here

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

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.

Sign up to request clarification or add additional context in comments.

1 Comment

hi ,MeBigFatGuy yes u r right that was the mistake i made thank u very much for solving..
1

You might want to parse from your buffer

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

instead of the inputstream.

Comments

0

Here's an example of how I parse 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);

.....

You can have a full example Here . I've started to developped this library with some methods that use this mechanism.

Tell me if it's help :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.