开发者

Error while parsing xml in java

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://ma开发者_StackOverflowps.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

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.


You might want to parse from your buffer

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

instead of the inputstream.


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜