我正在尝试从我的内部存储中读取和解析一些 XML:
private void readXML() {
XmlResourceParser p = Environment.getExternalStorageDirectory()
+ File.separator + "BootConfiguration";
try {
int nextEvent = p.next();
while (nextEvent != XmlPullParser.START_TAG) {
nextEvent = p.next();
}
processTag(p, p.getName(), new XMLCollector());
} catch (XmlPullParserException e) {
bootconfig = "ERROR: Failed to parse XML!\n" + e;
} catch (IOException e) {
bootconfig = "ERROR: IOException!\n" + e;
} finally {
p.close();
}
// TO-DO remove later
TextView t = (TextView) findViewById(R.id.bootconfig);
t.setText(bootconfig);
}
但是我收到一条错误消息:"Type mismatch: cannot convert from String to XmlResourceParser"
在线:
XmlResourceParser p = Environment.getExternalStorageDirectory()
我尝试将 XmlResourceParser 更改为字符串(如 eclipse 所建议的那样),但这只会在代码中进一步导致更多错误。
编辑:
我尝试了下面第一个答案中显示的方法,结果如下:
private void readXML() {
// XmlPullParser p = Environment.getExternalStorageDirectory()
// + File.separator + "JvsBootConfiguration";
//
InputStream istr = this.getAssets().open(Environment.getExternalStorageDirectory()
+ File.separator + "JvsBootConfiguration");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
p = factory.newPullParser();
p.setInput(istr, "UTF-8");
try {
int nextEvent = p.next();
while (nextEvent != XmlPullParser.START_TAG) {
nextEvent = p.next();
}
processTag(p, p.getName(), new XMLCollector());
} catch (XmlPullParserException e) {
bootconfig = "ERROR: Failed to parse XML!\n" + e;
} catch (IOException e) {
bootconfig = "ERROR: IOException!\n" + e;
} finally {
p.close();
}
但是我不确定我应该如何定义 p(直到我发现我只剩下'p 无法解析为变量')