about how to read the complicated xml file
2010-11-25 13:49
337 查看
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
;
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
;
相关文章推荐
- How to Read and Analyze the Email Header Fields and Information about SPF, DKIM, SpamAssassin
- How to Parse or Read XML File in Java >> XML Tutorial Example
- How To Read XML File In Java – (DOM Parser)
- a survey about how to make a file dynamically and offer it to download,but still not save the file!
- How to Read XMLDocument into a SQL Sever XML field / saving XML to database or filesystem...best method?
- How To Read XML File In Java – (SAX Parser)
- How To Read and Write XML Documents with GDataXML
- Howto let the SAX parser determine the encoding from the xml declaration?
- Read a Text File with VBA in Excel, and Write the Text to a Spreadsheet
- About:How to write the declaration of pointers and references rightly.
- Perl file checking --- How to get information about a file
- How to solve “add/remove operation is impossible, because the code element 'Cxxx' is read only” With
- svn: how to set the executable bit on a file?
- HOW TO Use the GCC specs file
- How to Copy an Assembly From the GAC to the File System
- How To Read a File From Your Application Bundle
- Flash / Flex Tutorial - How to Create a crossdomain.xml file
- How to read/write ini file
- HDOJ_ How can I read input data until the end of file ?
- How to resize a *.jpg image and save the result to a file