我正在尝试解析资源文件夹中的 XML 文件。这就是我想要做的 -
public void loadXMLtoDB() {
Resources resource = mContext.getResources();
dbInstance = new DBProvider();
dbInstance.onCreate();
try{
XmlResourceParser parser = resource.getXml(R.xml.default_apps);
parser.nextTag();
readApps(parser);
} catch (Exception e) {
e.printStackTrace();
}
}....
private void readApps(XmlResourceParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, null, "appsList");
while(parser.next() != XmlPullParser.END_TAG){
if(parser.getEventType() != XmlPullParser.START_TAG)
continue;
String name = parser.getName();
if(name.equals("appIcon")){
readAppIcon(parser);
}
else
skip(parser);
}
} .....
这是我的 XML 文件 -
<appsList xmlns:launcher="http://schemas.android.com/apk/res/com.example.kids" >
<appIcon>
<className>com.sec.android.app.camera.Camera</className>
<iconPosition>0</iconPosition>
<packageName>com.sec.android.app.camera</packageName>
<screen>0</screen>
</appIcon>
<appIcon>
<className>com.sec.android.gallery3d.app.Gallery</className>
<iconPosition>1</iconPosition>
<packageName>com.sec.android.gallery3d</packageName>
<screen>0</screen>
</appIcon>
</appsList>
这是我得到的例外: org.xmlpull.v1.XmlPullParserException:
Binary XML file line #-1: expected start or end tag (position:Binary XML file line #-1)
据我所知,XML 没问题——但有一个 XMLParsing 异常。