我试图从内部存储中读取和解析一些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更改为String(如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无法解析为变量')
答案 0 :(得分:-1)
Environment.getExternalStorageDirectory() + File.separator + "BootConfiguration";
返回一个String。 您不能将字符串分配给XmlResourceParser。
XmlResourceParser p = "some/directory";
相当于尝试分配:
int number = "some text";
你不能错过匹配类型。
在您的情况下,您需要调用一个返回XmlResourceParser的函数。 尝试: android: how to load xml file from assets directory? 要么 Create xml on the fly from a string