您的位置:首页 > 其它

about how to read the complicated xml file

2010-11-25 13:49 246 查看
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

;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: