Java parsing XML instance

Source: Internet
Author: User

/** <Br/> * Parse XML to return the set containing the game set (three game download lists ); <br/> * @ Param path <br/> * file path <br/> * @ return arraylist <xml_gamelistdata> set of game sets <br /> */<br/> Public arraylist <xml_gamelistdata> getgamearrylists (string path) <br/> throws timeoutexception {<br/> util. printlog ("= big arr game list =", "" + path ); <br/> // arraylist for installing arrlist <br/> arraylist <xml_gamelistdata> bigar R = new arraylist <xml_gamelistdata> (); <br/> // arraylisyt for game object loading <br/> arraylist <xml_gamelistdata> arr; <br/> domfac = documentbuilderfactory. newinstance (); // value <br/> try {<br/> dombuilder = domfac. newdocumentbuilder (); // value <br/> HTTP = (httpurlconnection) new URL (PATH ). openconnection (); <br/> HTTP. setconnecttimeout (10000); <br/> HTTP. setreadtimeout (10000); <br/> HTTP. setdoinput (true); <br/> HTTP. setdooutput (true); <br/> // obtain the network connection status <br/> NRC = http. getresponsecode (); <br/> If (NRC = httpurlconnection. http_ OK) {<br/> is = http. getinputstream (); <br/> Doc = dombuilder. parse (is); <br/> util. printlog ("= Doc =", "=" + DOC); <br/> // The root node is <list> node <br/> element root = (element) doc. getdocumentelement (); <br/> If (root = NULL | root. getnodetype ()! = Node. element_node) {<br/> return NULL; <br/>}< br/> // obtain all nodes in the next layer of the root node (including <vouchlist> <netlist> two nodes) <br/> nodelist mainitems = root. getchildnodes (); <br/> If (mainitems! = NULL) {<br/> // read each tag and lower-level tag in the XML file cyclically <br/> for (INT I = 0; I <mainitems. getlength (); I ++) {<br/> // 0 indicates the vouchlist node, and 1 indicates the netlist node. <br/> node seconditems = mainitems. item (I); <br/> arr = new arraylist (); <br/> // obtain the combination of game tags under itemlist <br/> nodelist gamelist = seconditems. getchildnodes (); <br/> If (gamelist! = NULL) {<br/> for (Int J = 0; j <gamelist. getlength (); j ++) {<br/> // list the J game nodes at the lower layer of node I <br/> node gameitem = gamelist. item (j); <br/> xml_gamelistdata gamedata = new xml_gamelistdata (); <br/> If (gameitem. getnodetype () = node. element_node) {<br/> gamedata. setid (gameitem. getattributes () <br/>. getnameditem ("ID "). getnodevalue (); <br/> gamedata. settype (gameitem. getattributes () <br/>. getnameditem ("Type") <br/>. getnodevalue (); <br/> gamedata. setishot (gameitem. getattributes () <br/>. getnameditem ("ishot") <br/>. getnodevalue (); <br/> gamedata. setname (gameitem. getattributes () <br/>. getnameditem ("name") <br/>. getnodevalue (); <br/>}< br/> arr. add (gamedata); <br/>}< br/> bigarr. add (ARR); <br/>}< br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> try {<br /> If (is! = NULL) {<br/> is. Close (); <br/>}< br/> If (HTTP! = NULL) {<br/> HTTP. disconnect (); <br/>}< br/>}catch (exception e) {<br/> E. printstacktrace (); <br/> return NULL; <br/>}< br/> return bigarr; <br/>}

Parse the XML file as follows:

 <List> <vouchlist> <game id = "4" type = "1" ishot = "1" name = "Angry Birds 0"/> <game id = "2" type = "1" ishot = "1" name = "Angry Birds 1"/> <game id = "3" type = "1" ishot = "1" name =" angry Birds 2 "/> <game id =" 6 "type =" 1 "ishot =" 1 "name =" wuziqi "/> <game id =" 7 "type = ""1" ishot = "1" name = "Angry Birds 3"/> <game id = "8" type = "1" ishot = "1" name = "Angry Birds bird 10 "/> </vouchlist> <netlist> <game id =" 4 "type =" 1 "ishot =" 1 "name =" Angry Bird 4 "/> <game id = "2" type = "1" ishot = "1" name = "Angry Birds 9"/> <game id = "3" type = "1" ishot =" "0" name = "Angry Birds 5"/> <game id = "6" type = "1" ishot = "1" name = "Angry Birds 8"/> <game id = "7" type = "1" ishot = "1" name = "Angry Birds 6"/> <game id = "8" type = "1" ishot = "1" name = "Angry Birds 7"/> </netlist> <locallist/> </List>
 

 

 

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.