我已经能够成功地将数据引入单个表,但需要为多个表提供数据。是否可以从单个xml文件中执行此操作?
//Open xml file
XmlResourceParser _xml = res.getXml(R.xml.animals_records);
try
{
//Check for end of document
int eventType = _xml.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
//Search for record tags
if ((eventType == XmlPullParser.START_TAG) &&(_xml.getName().equals("record"))){
//Record tag found, now get values and insert record
String _Title = _xml.getAttributeValue(null, TITLE);
String _Color = _xml.getAttributeValue(null, COLOR, 0);
_Values.put(TITLE, _Title);
_Values.put(COLOR, _Color);
db.insert(TABLENAME1, null, _Values);
}
if ((eventType == XmlPullParser.START_TAG) &&(_xml.getName().equals("trees"))){
//Record tag found, now get values and insert record
String _Title = _xml.getAttributeValue(null, FAMILY);
String _Color = _xml.getAttributeValue(null, SPECIES, 0);
_Values.put(FAMILY, _Title);
_Values.put(SPECIES, _Color);
db.insert(TABLENAME2, null, _Values);
}
eventType = _xml.next();
}
}
使用看起来像这样的XML:
<animals>
<record title="Dog" color="Brown" />
<record title="Cat" color="Gray" />
<record title="Rabbit" color="White" />
<record title="Spider" color="Black" />
<trees family="Hardwood" species="Oak" />
<trees family="Soft" color="Pine" />
</animals>
有什么想法吗?明显的错误?