假设我有这样的文件 XML:
<?xml version="1.0"?>
<P>
<Content>
<id>1016576</id>
<date>20.08.2012</date>
<placeOfBirth>KUALA LUMPUR</placeOfBirth>
</Content>
<Content>
<id>1016620</id>
<date>20.08.2012</date>
<placeOfBirth>SINGAPORE</placeOfBirth>
</Content>
<Content>
<id>1020907</id>
<date>20.08.2012</date>
<placeOfBirth>SINGAPORE</placeOfBirth>
</Content>
</P>
我想解析所有文本并插入到具有 _id、date 和 placeOfBirth 列的数据库表中。我试过这个:
Activity activity1 = this;
String str="";
Resources res = activity1.getResources();
XmlResourceParser xmlPharser = res.getXml(R.xml.fileXML);
String id,date,pob;
//database
final databaseHelper myDbHelper = new databaseHelper(this);
myDbHelper.open();
//insert into table while parsing xml
try {
xmlPharser.next();
int eventType = xmlPharser.getEventType();
String event = ""+eventType;
Log.d("Event", event);
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_TAG )
{
if( xmlPharser.getName() == "id")
{
id=xmlPharser.getText();
}
else if ( xmlPharser.getName() == "date" )
{
date = xmlPharser.getText();
}
else if ( xmlPharser.getName() == "placeOfBirth" )
{
pob = xmlPharser.getText();
}
myDbHelper.insertData(id,date,pob);
myDbHelper.close();
}
eventType = xmlPharser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
它不会显示任何错误 LogCat,但不会在每个 START_TAG 处测试条件。
我该如何解决这个问题......所有答案将不胜感激......谢谢