Last blog,i had writed how to read one xml with a lot of instances,in which the instance is so easy,just with serveral members,now this blog will start to read more complicated xml ,the instance in which is not only the easy memebr,also with list<child-instance>.
1 .the xml is like:
<root>
<parent>
<id></id>
<name></name>
<child>
<id></id>
<name></name>
</child>
<child>
<id></id>
<name></name>
</child>
<child>
<id></id>
<name></name>
</child>
.
.
.
</parent>
<parent>
<id></id>
<name></name>
<child>
<id></id>
<name></name>
</child>
<child>
<id></id>
<name></name>
</child>
<child>
<id></id>
<name></name>
</child>
.
.
.
</parent>
</root>
2. setup class ,based on the parent and child
public class parent{
private String parent_id;
private String parent_name;
List<child> childs = new ArrayList<child>();
public parent(String parent_id,String parent_name,List<child> childs ){
this.parent_id = parent_id;
this.parent_name = parent_name;
this.childs = childs;
}
// get and set
.............................
}
public class child{
private String child_id;
private String child_name;
public child (String child_id,String child_name){
this.child_id = child_id;
this.child_name = child_name;
}
// get and set
.............................
}
3 . Read xml also using the xml pull
case XmlPullParser. END_TAG :
// 判断 child 的 list
if (xpp.getName().equalsIgnoreCase( "child" ) && child != null ) {
parent .setFilesizes( child );
size = null ;
}
// 包在外面的 tag—parent
if (xpp.getName().equalsIgnoreCase( "parent" ) && parent!= null && parents!= null ) {
parents .add( parent );
parent = null ;
}
else if (xpp.getName().equalsIgnoreCase( "root" )){
done = true ;