/**<br /> * 解析xml返回裝有game集合的集合(三個遊戲下載列表);<br /> *<br /> * @param path<br /> * 檔案路徑<br /> * @return ArrayList<ArrayList<XML_GameListData>> game集合的集合<br /> */<br />public ArrayList<ArrayList<XML_GameListData>> getGameArryLists(String path)<br />throws TimeoutException {<br />Util.printLog("==big arr game list==", "" + path);<br />// 裝arrlist的arraylist<br />ArrayList<ArrayList<XML_GameListData>> bigarr = new ArrayList<ArrayList<XML_GameListData>>();<br />// 裝game對象的arraylisyt<br />ArrayList<XML_GameListData> arr;<br />domfac = DocumentBuilderFactory.newInstance();// 有值<br />try {<br />dombuilder = domfac.newDocumentBuilder();// 有值<br />http = (HttpURLConnection) new URL(path).openConnection();<br />http.setConnectTimeout(10000);<br />http.setReadTimeout(10000);<br />http.setDoInput(true);<br />http.setDoOutput(true);<br />// 得到網路連接狀態<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 />// 根節點為<list>節點<br />Element root = (Element) doc.getDocumentElement();<br />if (root == null || root.getNodeType() != Node.ELEMENT_NODE) {<br />return null;<br />}<br />// 得到根節點下一層所有節點(包括<vouchlist><netlist>兩個節點)<br />NodeList mainItems = root.getChildNodes();<br />if (mainItems != null) {<br />// 迴圈讀取xml檔案中每個標籤和下層標籤<br />for (int i = 0; i < mainItems.getLength(); i++) {<br />// 0為vouchlist節點,1為netlist節點;<br />Node secondItems = mainItems.item(i);<br />arr = new ArrayList();<br />// 獲得itemlist下game標籤的結合<br />NodeList gamelist = secondItems.getChildNodes();<br />if (gamelist != null) {<br />for (int j = 0; j < gamelist.getLength(); j++) {<br />// list下第i個節點下一層的第j個game節點<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 />}<br />bigarr.add(arr);<br />}<br />}<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 />}<br />return bigarr;<br />}
解析xml檔案為:
<list><vouchlist><game id="4" type="1" ishot="1" name="憤怒的小鳥0"/><game id="2" type="1" ishot="1" name="憤怒的小鳥1"/><game id="3" type="1" ishot="1" name="憤怒的小鳥2"/><game id="6" type="1" ishot="1" name="五子棋"/><game id="7" type="1" ishot="1" name="憤怒的小鳥3"/><game id="8" type="1" ishot="1" name="憤怒的小鳥10"/></vouchlist><netlist><game id="4" type="1" ishot="1" name="憤怒的小鳥4"/><game id="2" type="1" ishot="1" name="憤怒的小鳥9"/><game id="3" type="1" ishot="0" name="憤怒的小鳥5"/><game id="6" type="1" ishot="1" name="憤怒的小鳥8"/><game id="7" type="1" ishot="1" name="憤怒的小鳥6"/><game id="8" type="1" ishot="1" name="憤怒的小鳥7"/></netlist><locallist/></list>