我有以下XML
<?xml version="1.0" encoding="UTF-8"?>
<DataList Name="DistributorList">
<Distributor Id="Select" Name="Select">
<Town Id="Select" Name="Select">
<Beat Id="Select" Name="Select">
<Dealer Id="Select" Name="Select" />
</Beat>
</Town>
</Distributor>
<Distributor Id="107" Name="Royal Motors">
<Town Id="Select" Name="Select">
<Beat Id="Select" Name="Select">
<Dealer Id="Select" Name="Select" />
</Beat>
</Town>
<Town Id="21" Name="Gurgaon">
<Beat Id="Select" Name="Select">
<Dealer Id="Select" Name="Select" />
</Beat>
</Town>
</Distributor>
<Distributor Id="109" Name="ALI DISTRIBUTOR POINT">
<Town Id="Select" Name="Select">
<Beat Id="Select" Name="Select">
<Dealer Id="Select" Name="Select" />
</Beat>
</Town>
<Town Id="21" Name="Gurgaon">
<Beat Id="Select" Name="Select">
<Dealer Id="Select" Name="Select" />
</Beat>
</Town>
</Distributor>
<Distributor Id="144" Name="Goyal Accessories">
<Town Id="Select" Name="Select">
<Beat Id="Select" Name="Select">
<Dealer Id="Select" Name="Select" />
</Beat>
</Town>
<Town Id="22" Name="Faridabad">
<Beat Id="Select" Name="Select">
<Dealer Id="Select" Name="Select" />
</Beat>
<Beat Id="1" Name="Area1">
<Dealer Id="Select" Name="Select" />
<Dealer Id="24964" Name="Kanhaiya Dial " />
<Dealer Id="24964" Name="Kanhaiya Dial " />
<Dealer Id="24965" Name="B.K.Mobile Service Centre" />
<Dealer Id="24965" Name="B.K.Mobile Service Centre" />
<Dealer Id="24966" Name="Narayan Telecom" />
<Dealer Id="24966" Name="Narayan Telecom" />
<Dealer Id="24967" Name="Pawan Telecom" />
<Dealer Id="24967" Name="Pawan Telecom" />
<Dealer Id="24968" Name="Sri Ram Communication" />
<Dealer Id="24968" Name="Sri Ram Communication" />
</Beat>
</Town>
</Distributor>
</DataList>
这是XML解析代码
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
if (event == XmlPullParser.START_DOCUMENT) {
System.out.println("Start document");
} else if (event == XmlPullParser.END_DOCUMENT) {
System.out.println("End document");
} else if (event == XmlPullParser.START_TAG) {
System.out.println("Start tag " + myParser.getName());
if (myParser.getName().equals("DataList")) {
Log.e("data list ",
"" + myParser.getAttributeValue(null, "Name"));
} else if (myParser.getName().equals("Distributor")) {
Log.e("dis name ",
"" + myParser.getAttributeValue(null, "Name"));
Log.e("dis id ",
"" + myParser.getAttributeValue(null, "Id"));
} else if (myParser.getName().equals("Town")) {
Log.e("town name ",
"" + myParser.getAttributeValue(null, "Name"));
Log.e("Town id ",
"" + myParser.getAttributeValue(null, "Id"));
} else if (myParser.getName().equals("Beat")) {
Log.e("beat name ",
"" + myParser.getAttributeValue(null, "Name"));
Log.e("beat id ",
"" + myParser.getAttributeValue(null, "Id"));
} else if (myParser.getName().equals("Dealer")) {
Log.e("dealer name ",
"" + myParser.getAttributeValue(null, "Name"));
Log.e("delaer id ",
"" + myParser.getAttributeValue(null, "Id"));
}
} else if (event == XmlPullParser.END_TAG) {
System.out.println("End tag " + myParser.getName());
} else if (event == XmlPullParser.TEXT) {
System.out.println("Text " + myParser.getText());
}
event = myParser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
}
我需要在Android中的单个或多个SQLite表中保存XML中的数据,无论哪个最可行。 可以有多个城镇,节拍和经销商数据。
表格行
dist_id
dist_name
town_id
town_name
beat_id
beat_name
dealer_id
dealer_name
如何保存数据?我没有得到任何关于它的线索。 该表应填充XML中的相应数据。
答案 0 :(得分:1)
为数据创建一个类..它具有以下数据类型..
class myData{
dist_id
dist_name
town_id
town_name
beat_id
beat_name
dealer_id
dealer_name
}
您创建了一个ArrayList
<强>更新强>
class myData{
dist_id
dist_name
town_id
town_name
beat_id
beat_name
ArrayList<Dealer>
}
class Dealer{
dealer_id
dealer_name
}
答案 1 :(得分:0)
使用以下课程设计:
class DataList
{
ArrayList<Data> data;
}
class Data
{
String distributorId;
String distributorName;
String townId;
String townName;
String beatId;
String beatName;
DealerData dealerData;
}
class DealerData
{
//ArrayList of DealerId, DealerName
ArrayList<String, String> dealerData;
}
从xml解析每个分发者记录并将其添加到DataList。完成解析后,遍历DataList
的每个数据并将其添加到表中。要解决拥有多个Dealer
数据的问题,请为每个经销商添加单独的行。
在此之后,表格中的行将如下所示:
distributionId distributionName townId townName beatId beatName dealerId1 dealerName1
distributionId distributionName townId townName beatId beatName dealerId2 dealerName2