2011-03-30 3 views
2

임 ..오류 동안

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

을 구글 지오 코드 API에 의해 반환 된 XML을 구문 분석을 시도하지만, 구문 분석하는 동안 메신저 다음과 같은 오류를 얻는 것은 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(); 
    } 

이에 MW에게 도움을 제안 해주십시오 ..

u를 감사드립니다.

+0

는 [이 게시물]를 참조 (http://stackoverflow.com/questions/708327/premature-end-of-file-error-when-java-read-and-의 경우

것은 말해 writes-xml-data-files) –

+0

안녕하세요 존이 답장을 보내 주셔서 감사합니다. 링크 u는 잘못된 것이 아니라면, 동기화 문제와 관련하여 더 많은 것을 제안합니다. 그러나 나는 그다지 도움이되지 못했습니다 .. – bHaRaTh

+0

@ user593424 : XML 태그를 닫을 수 없을 수도 있습니다. –

답변

5

디버깅 코드가 문제입니다. 당신은 모든 과거의 데이터를 스트림을 전진 여기

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

XML을 표시하는 문서를 읽고, 당신은 파서 다시 읽어보십시오.

디버그 코드를 제거하면 제대로 작동합니다.

......

 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을 가질 수

+0

안녕하세요, MeBigFatGuy 예 u r 맞습니다. 제가 실수로 해결해 주신 것에 대해 감사드립니다. – bHaRaTh

1

당신은 당신의 버퍼 대신의 InputStream의

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

에서 구문 분석 할 수 있습니다.

0

는 여기에 Google지도 API를 구문 분석하는 방법의 예입니다. 이 메커니즘을 사용하는 몇 가지 방법으로이 라이브러리를 개발하기 시작했습니다. 그것은 도움이 :)

관련 문제