我有一个需要 dom 解析的应用程序,该应用程序在我同事的机器上运行良好,但在我的机器上它正在抛出 saxexception(意外令牌)。知道为什么日食行为怪异。以下是我如何读取我的 xml 文件的方法。
private String readFile() throws Exception{
AssetManager assetManager = activity.getAssets();
InputStream ims = assetManager.open("temp/MyXml.xml");
StringBuffer fileContent = new StringBuffer("");
byte[] buffer = new byte[1024];
while (ims.read(buffer) != -1) {
fileContent.append(new String(buffer));
}
return fileContent.toString();
}
下面是解析的方法。
private Document getDomElement(String xml) throws Exception {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
throw e;
} catch (SAXException e) {
throw e;
} catch (IOException e) {
throw e;
}
// return DOM
return doc;
}
和堆栈跟踪
11-05 17:16:30.200: W/System.err(12283): org.xml.sax.SAXParseException: Unexpected token (position:TEXT
11-05 17:16:30.200: W/System.err(12283): URRICANE DISH D...@2:237 in java.io.StringReader@2bdda808)
11-05 17:16:30.200: W/System.err(12283): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146)
11-05 17:16:30.200: W/System.err(12283): at org.col.gsso.sap.parser.InputDataXMLParser.getDomElement(InputDataXMLParser.java:510)
11-05 17:16:30.200: W/System.err(12283): at org.col.gsso.sap.parser.InputDataXMLParser.saveInputFiles(InputDataXMLParser.java:106)
11-05 17:16:30.200: W/System.err(12283): at com.gps.colpal.ToPalmDownloadAsycTask.doInBackground(ToPalmDownloadAsycTask.java:103)
11-05 17:16:30.200: W/System.err(12283): at com.gps.colpal.ToPalmDownloadAsycTask.doInBackground(ToPalmDownloadAsycTask.java:1)
11-05 17:16:30.200: W/System.err(12283): at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-05 17:16:30.200: W/System.err(12283): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-05 17:16:30.200: W/System.err(12283): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-05 17:16:30.200: W/System.err(12283): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-05 17:16:30.200: W/System.err(12283): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-05 17:16:30.200: W/System.err(12283): at java.lang.Thread.run(Thread.java:856)
11-05 17:16:30.250: W/IInputConnectionWrapper(12283): finishComposingText on inactive InputConnection