歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android DOM 解析XML方式

Android DOM 解析XML方式

日期:2017/3/1 10:45:09   编辑:Linux編程

首先自己創建一個xml文件:DomTest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <classes>
  3. <group name="一年級" num="10">
  4. <person name="小明" age="7">
  5. <chinese>
  6. 語文80
  7. </chinese>
  8. <english>
  9. 英語89
  10. </english>
  11. </person>
  12. <person name="小強" age="8">
  13. <chinese>
  14. 語文90
  15. </chinese>
  16. <english>
  17. 英語99
  18. </english>
  19. </person>
  20. </group>
  21. <group name="二年級" num="20">
  22. <person name="小文" age="8">
  23. <chinese>
  24. 語文85
  25. </chinese>
  26. <english>
  27. 英語95
  28. </english>
  29. </person>
  30. <person name="小中" age="9">
  31. <chinese>
  32. 語文80
  33. </chinese>
  34. <english>
  35. 英語90
  36. </english>
  37. </person>
  38. </group>
  39. </classes>

解析出來的結果顯示如下圖:

下面來分析源代碼:

  1. /**
  2. * 用dom方式 解析xml 文件
  3. * @param fileName
  4. */
  5. private String domXmlParse(String fileName) {
  6. String str="";
  7. // xml文檔創建工廠
  8. DocumentBuilderFactory docFactory = DocumentBuilderFactory
  9. .newInstance();
  10. // xml文檔創建實例
  11. DocumentBuilder docBuilder;
  12. // xml文檔
  13. Document doc = null;
  14. InputStream inStream = null;
  15. try {
  16. docBuilder = docFactory.newDocumentBuilder();
  17. // 從assets文件夾下獲取文件 轉換成輸入流
  18. inStream = this.getResources().getAssets().open(fileName);
  19. doc = docBuilder.parse(inStream);
  20. // 獲取xml跟元素
  21. Element rootEle = doc.getDocumentElement();
  22. // 二級父元素的list列表
  23. NodeList groupNode = rootEle.getElementsByTagName("group");
  24. // NodeList childNode = rootEle.getElementsByTagName("person");
  25. // 遍歷Classe下所有的group
  26. for (int i = 0; i < groupNode.getLength(); i++) {
  27. Element groupEle = (Element) groupNode.item(i);
  28. String groupName = groupEle.getAttribute("name");
  29. String num = groupEle.getAttribute("num");
  30. strstr =str+"name ="+groupName+" num = "+num+"\n";
  31. Log.e("xml", "name = " + groupName + " num = " + num);
  32. // NodeList personNode = groupNode.item(i).getChildNodes();
  33. NodeList personNode = groupEle.getElementsByTagName("person");
  34. // 遍歷group下的所有person
  35. for (int j = 0; j < personNode.getLength(); j++) {
  36. Element personEle = (Element) personNode.item(j);
  37. String name = personEle.getAttribute("name");
  38. String age = personEle.getAttribute("age");
  39. strstr =str+"personName ="+name+" personAge = "+age+"\n";
  40. Log.e("xml", "name = " + name + " age = " + age);
  41. Element chineseEle = (Element) personEle
  42. .getElementsByTagName("chinese").item(0);
  43. Element englistEle = (Element) personEle
  44. .getElementsByTagName("english").item(0);
  45. String chinese = chineseEle.getFirstChild().getNodeValue();
  46. String english = englistEle.getFirstChild().getNodeValue();
  47. strstr =str+"chinese = "+chinese+" english = "+english+"\n";
  48. Log.e("xml", "chinese = " + chinese + " english = "
  49. + english);
  50. }
  51. }
  52. } catch (ParserConfigurationException e1) {
  53. e1.printStackTrace();
  54. } catch (IOException e) {
  55. e.printStackTrace();
  56. } catch (SAXException e) {
  57. e.printStackTrace();
  58. }
  59. return str;
  60. }

為 XML 文檔的已解析版本定義了一組接口。解析器讀入整個文檔,然後構建一個駐留內存的樹結構,然後代碼就可以使用 DOM 接口來操作這個樹結構。優點:整個文檔樹在內存中,便於操作;支持刪除、修改、重新排列等多種功能;缺點:將整個文檔調入內存(包括無用的節點),浪費時間和空間;使用場合:一旦解析了文檔還需多次訪問這些數據;硬件資源充足(內存、CPU)。

完整工程的下載地址:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /pub/2011/12/27/Android DOM 解析XML方式/

Copyright © Linux教程網 All Rights Reserved