XML parsing multiple nodes

Source: Internet
Author: User

XML:
<Friendshares>
<Totalcount> 352 </totalcount>
<Friendshare>
<Code> xxxxxxx </code>
<Date> 2010-01-15t00: 00: 00 + 08: 00 </date>
<Friendid> 499 </friendid>
<Movie>
<Code> xxxxxx </code>
<Contentid> 89718 </contentid>
<Moviename> Mobile Force Police instance </moviename>
<Picpath>/20090817/189736 .gif</picpath>
</Movie>
<Nickname> xjmy </nickname>
<Recommendid> 32769 </recommendid>
<Uid> 499 </uid>
</Friendshare>
<Friendshare>
<Code> xxxxxxx </code>
<Date> 2010-01-15t00: 00: 00 + 08: 00 </date>
<Friendid> 499 </friendid>
<Movie>
<Code> xxxxxx </code>
<Contentid> 89718 </contentid>
<Moviename> Mobile Force Police instance </moviename>
<Picpath>/20090817/189736 .gif</picpath>
</Movie>
<Nickname> xjmy </nickname>
<Recommendid> 32769 </recommendid>
<Uid> 499 </uid>
</Friendshare>
</Friendshares>

Java code:

Document Doc;
Try {
Doc = documentbuilderfactory. newinstance (). newdocumentbuilder ()
. Parse (New inputsource (New stringreader (XML )));

Nodelist NL = Doc. getelementsbytagname ("totalcount ");
If (Doc. getelementsbytagname ("totalcount"). Item (0 )! = NULL ){
If (Doc. getelementsbytagname ("totalcount"). Item (0). getfirstchild ()! = NULL ){
Int totalcount = integer. parseint (Doc. getelementsbytagname ("totalcount"). Item (0). getfirstchild (). getnodevalue ());
NL = Doc. getelementsbytagname ("friendshare ");
For (INT I = 0; I <NL. getlength (); I ++ ){
Iptvmembers members = new iptvmembers ();
Members. setcount (totalcount );
If (Doc. getelementsbytagname ("date"). Item (I )! = NULL ){
If (Doc. getelementsbytagname ("date"). Item (I). getfirstchild ()! = NULL ){
String date = Doc. getelementsbytagname ("date"). Item (I). getfirstchild (). getnodevalue ();
Date = date. replaceall ("T ","");
Int Index = date. indexof ("+ ");
Date = date. substring (0, index );
Members. setcreatedate (date );
}
}
If (Doc. getelementsbytagname ("nickname"). Item (I )! = NULL ){
If (Doc. getelementsbytagname ("nickname"). Item (I). getfirstchild ()! = NULL ){
Members. setnickname (Doc. getelementsbytagname ("nickname"). Item (I). getfirstchild (). getnodevalue ());
}
}
If (Doc. getelementsbytagname ("contentid"). Item (I )! = NULL ){
If (Doc. getelementsbytagname ("contentid"). Item (I). getfirstchild ()! = NULL ){
Members. setcontentid (Doc. getelementsbytagname ("contentid"). Item (I). getfirstchild (). getnodevalue ());
}
}
If (Doc. getelementsbytagname ("moviename"). Item (I )! = NULL ){
If (Doc. getelementsbytagname ("moviename"). Item (I). getfirstchild ()! = NULL ){
Members. setmoviename (Doc. getelementsbytagname ("moviename"). Item (I). getfirstchild (). getnodevalue ());
}
}

// The main note here is that there is also a movie node under the friendshare in the middle.
Code. Since there are two code subnodes in friendshare, when code is obtained in the format of moviename, the error will be doubled.
Code value. At the beginning, I thought that movie would be looping like friendshare, but after multiple experiments, I still could not get the correct code data. Change to the following red font:
The code value can be obtained in the correct order.

If (Doc. getelementsbytagname ("movie"). Item (I )! = NULL ){
If (Doc. getelementsbytagname ("movie"). Item (I). getchildnodes (). Item (0 )! = NULL ){
Members. setmedicode (Doc. getelementsbytagname ("movie"). Item (I). getchildnodes (). Item (0). getfirstchild (). getnodevalue ());
String ur = "cmd = play & User =" + member + "& program = <mediacode> ";
Members. setcode ("" + new
Base64 (). getbase64 (UR + Doc. getelementsbytagname ("movie "). item (I ). getchildnodes (). item (0 ). getfirstchild (). getnodevalue () + "</mediacode> "));
}
}

If (Doc. getelementsbytagname ("friendid"). Item (I )! = NULL ){
If (Doc. getelementsbytagname ("friendid"). Item (I). getfirstchild ()! = NULL ){
Members. setfriendname (Doc. getelementsbytagname ("friendid"). Item (I). getfirstchild (). getnodevalue ());
}
}

If (Doc. getelementsbytagname ("picpath"). Item (I )! = NULL ){
If (Doc. getelementsbytagname ("picpath"). Item (I). getfirstchild ()! = NULL ){
Members. setpicpath (Doc. getelementsbytagname ("picpath"). Item (I). getfirstchild (). getnodevalue ());
Members. setpath (config. Get ("url. fileserver") + members. getpicpath ());
}
}
If (Doc. getelementsbytagname ("uid"). Item (I )! = NULL ){
If (Doc. getelementsbytagname ("uid"). Item (I). getfirstchild ()! = NULL ){
Members. setuid (integer. parseint (Doc. getelementsbytagname ("uid"). Item (I). getfirstchild (). getnodevalue ()));
}
}
If (listmembers! = NULL & listmembers. Size ()> 0 ){
Boolean flg = false;
For (INT n = 0; n <listmembers. Size (); N ++ ){
Flg = false;
If (listmembers. Get (N). getcontentid (). Equals (members. getcontentid ()))
{
Flg = true;
Listmembers. Get (N). setfriendname (listmembers. Get (N). getfriendname () + "," + members. getfriendname ());
}
}
If (flg = false ){
Listmembers. Add (members );
}
} Else
{
Listmembers. Add (members );
}
}
}
}
} Catch (saxexception E)
{
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
} Catch (parserconfigurationexception e ){
E. printstacktrace ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.