我有一個XML文件託管在我的Google驅動器上。它有一些我的Android應用程序需要運行的數據。所以,我決定測試我是否可以從文件中獲取數據並進行測試SAXParser
。不過,我得到一個SAXParseException
:SAXParseException:文件過早結束
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
at testing.TestSAX.processFeed(TestSAX.java:54)
at testing.TestSAX.main(TestSAX.java:40)
雖然,同樣的代碼工作正常,比如說RSS源,它崩潰了谷歌驅動文件。
這裏是我打開流:
try{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
InputStream stream = new URL(url).openStream();
Reader r = new InputStreamReader(stream,"UTF-8");
InputSource source = new InputSource(stream);
source.setEncoding("UTF-8");
parser.parse(source, this);
}catch(Exception e){
errorOccured = true;
e.printStackTrace();
}
如何解決這個問題?
http://developer.android.com/training/basics/network-ops/xml.html。使用xmlpullparser而不是sax。從文檔引用「推薦使用XmlPullParser,這是一種高效且可維護的方式來解析Android上的XML。」 – Raghunandan
也許你得到空字符串響應由於這個你得到EOF –
@HarishGodara有沒有辦法解決它? –