JAVA解析Xml執行個體

來源:互聯網
上載者:User

/**<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>
 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.