Warum erhalte ich die Fehlermeldung "Vorzeitiges Ende der Datei"?
Ich versuche zu analysieren undXML response
, aber ich versage kläglich. Ich dachte anfangs, dass diexml
wurde in der Antwort einfach nicht zurückgegeben, daher habe ich den folgenden Code mit einem direkten Link zu meinem Code erstelltxml
Datei online. Ich kann das ausdruckenXML
ohne Probleme zu screenen. Wenn ich jedoch meine Analysemethode aufrufe, erhalte ichVorzeitiges Ende der Datei.
Es funktioniert, wenn ich die URL direkt übergebe:
builder.parse ("");schlägt aber fehl, wenn ich einen InputStream übergebe:
builder.parse (connection.getInputStream ());
<code> try { URL url = new URL(xml); URLConnection uc = url.openConnection(); HttpURLConnection connection = (HttpURLConnection )uc; connection.setDoInput(true); connection.setDoOutput(true); InputStream instream; InputSource source; //get XML from InputStream if(connection.getResponseCode()>= 200){ connection.connect(); instream = connection.getInputStream(); parseDoc(instream); } else{ instream = connection.getErrorStream(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } static void parseDoc(InputStream instream) throws ParserConfigurationException, SAXException, IOException{ BufferedReader buff_read = new BufferedReader(new InputStreamReader(instream,"UTF-8")); String inputLine = null; while((inputLine = buff_read.readLine())!= null){ System.out.println(inputLine); } DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance(); factory.isIgnoringElementContentWhitespace(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(instream); } </code>
Die Fehler, die ich erhalte:
<code> [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 com.ameba.api.network.MainApp.parseDoc(MainApp.java:78) at com.ameba.api.network.MainApp.main(MainApp.java:41) </code>