XML文件

1 <html> 2 <head> 3 <title>dom4j解析一個例子</title> 4 <script> 5 <username>yangrong</username> 6 <password>123456</password> 7 </script> 8 </head> 9 <body> 10 <result>0</result> 11 <form> 12 <banlce>1000</banlce> 13 <subID>36242519880716</subID> 14 </form> 15 </body> 16 </html>
解析代碼:
1 package xml; 2 3 import java.io.File; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.Map; 7 8 import org.dom4j.Document; 9 import org.dom4j.DocumentException; 10 import org.dom4j.DocumentHelper; 11 import org.dom4j.Element; 12 import org.dom4j.io.SAXReader; 13 14 public class TestDom4j { 15 public void readStringXml(String xml) { 16 Document doc = null; 17 try { 18 // 讀取並解析XML文檔,SAXReader就是一個管道,用一個流的方式,把xml文件讀出來 19 SAXReader reader = new SAXReader(); 20 doc = reader.read(new File(xml)); 21 22 Element rootElt = doc.getRootElement(); // 獲取根節點 23 System.out.println("根節點:" + rootElt.getName()); // 拿到根節點的名稱 24 25 Iterator<Element> iter = rootElt.elementIterator("head"); // 獲取根節點下的子節點head 26 27 // 遍歷head節點 28 while (iter.hasNext()) { 29 Element recordEle = (Element) iter.next(); 30 String title = recordEle.elementTextTrim("title"); // 拿到head節點下的子節點title值 31 System.out.println("title:" + title); 32 33 Iterator<Element> iters = recordEle.elementIterator("script"); // 獲取子節點head下的子節點script 34 35 // 遍歷Header節點 36 while (iters.hasNext()) { 37 Element itemEle = (Element) iters.next(); 38 39 String username = itemEle.elementTextTrim("username"); // 拿到head下的子節點script下的字節點username的值 40 String password = itemEle.elementTextTrim("password"); 41 42 System.out.println("username:" + username); 43 System.out.println("password:" + password); 44 } 45 } 46 Iterator<Element> iterss = rootElt.elementIterator("body"); // 獲取根節點下的子節點body 47 // 遍歷body節點 48 while (iterss.hasNext()) { 49 Element recordEless = (Element) iterss.next(); 50 String result = recordEless.elementTextTrim("result"); // 拿到body節點下的子節點result值 51 System.out.println("result:" + result); 52 53 Iterator<Element> itersElIterator = recordEless.elementIterator("form"); // 獲取子節點body下的子節點form 54 // 遍歷form節點 55 while (itersElIterator.hasNext()) { 56 Element itemEle = (Element) itersElIterator.next(); 57 58 String banlce = itemEle.elementTextTrim("banlce"); // 拿到body下的子節點form下的子節點banlce的值 59 String subID = itemEle.elementTextTrim("subID"); 60 61 System.out.println("banlce:" + banlce); 62 System.out.println("subID:" + subID); 63 } 64 } 65 } catch (DocumentException e) { 66 e.printStackTrace(); 67 } catch (Exception e) { 68 e.printStackTrace(); 69 } 70 } 71 72 /** 73 * @description 將xml字符串轉換成map 74 * @param xml 75 * @return Map 76 */ 77 public static Map<String,String> readStringXmlOut(String xml) { 78 Map<String,String> map = new HashMap<String,String>(); 79 Document doc = null; 80 try { 81 // 將字符串轉為XML 82 doc = DocumentHelper.parseText(xml); 83 // 獲取根節點 84 Element rootElt = doc.getRootElement(); 85 // 拿到根節點的名稱 86 System.out.println("根節點=" + rootElt.getName()); 87 88 // 獲取根節點下的子節點head 89 Iterator<Element> iter = rootElt.elementIterator("head"); 90 // 遍歷head節點 91 while (iter.hasNext()) { 92 93 Element recordEle = (Element) iter.next(); 94 // 拿到head節點下的子節點title值 95 String title = recordEle.elementTextTrim("title"); 96 map.put("title", title); 97 // 獲取子節點head下的子節點script 98 Iterator<Element> iters = recordEle.elementIterator("script"); 99 // 遍歷Header節點下的Response節點 100 while (iters.hasNext()) { 101 Element itemEle = (Element) iters.next(); 102 // 拿到head下的子節點script下的字節點username的值 103 String username = itemEle.elementTextTrim("username"); 104 String password = itemEle.elementTextTrim("password"); 105 106 map.put("username", username); 107 map.put("password", password); 108 } 109 } 110 111 //獲取根節點下的子節點body 112 Iterator<Element> iterss = rootElt.elementIterator("body"); 113 // 遍歷body節點 114 while (iterss.hasNext()) { 115 Element recordEless = (Element) iterss.next(); 116 // 拿到body節點下的子節點result值 117 String result = recordEless.elementTextTrim("result"); 118 // 獲取子節點body下的子節點form 119 Iterator<Element> itersElIterator = recordEless.elementIterator("form"); 120 // 遍歷Header節點下的Response節點 121 while (itersElIterator.hasNext()) { 122 Element itemEle = (Element) itersElIterator.next(); 123 // 拿到body下的子節點form下的字節點banlce的值 124 String banlce = itemEle.elementTextTrim("banlce"); 125 String subID = itemEle.elementTextTrim("subID"); 126 127 map.put("result", result); 128 map.put("banlce", banlce); 129 map.put("subID", subID); 130 } 131 } 132 } catch (DocumentException e) { 133 e.printStackTrace(); 134 } catch (Exception e) { 135 e.printStackTrace(); 136 } 137 return map; 138 } 139 140 public static void main(String[] args) { 141 142 // 下面是需要解析的xml字符串例子 143 String xmlString = "<html>" + "<head>" + "<title>dom4j解析一個例子</title>" 144 + "<script>" + "<username>yangrong</username>" 145 + "<password>123456</password>" + "</script>" + "</head>" 146 + "<body>" + "<result>0</result>" + "<form>" 147 + "<banlce>1000</banlce>" + "<subID>36242519880716</subID>" 148 + "</form>" + "</body>" + "</html>"; 149 150 System.out.println("E://xml//hbm.xml中的值:"); 151 TestDom4j test = new TestDom4j(); 152 test.readStringXml("E://xml//hbm.xml"); 153 154 System.out.println(); 155 156 System.out.println("XML字符串xmlString中的值:"); 157 Map<String,String> map = readStringXmlOut(xmlString); 158 Iterator<String> iters = (Iterator)map.keySet().iterator(); 159 while (iters.hasNext()) { 160 String key = iters.next(); // 拿到鍵 161 String val = map.get(key).toString(); // 拿到值 162 163 System.out.println(key + "=" + val); 164 } 165 } 166 167 }
兩種方式的輸出結果是一樣的:
E://xml//hbm.xml中的值:
根節點:html
title:dom4j解析一個例子
username:yangrong
password:123456
result:0
banlce:1000
subID:36242519880716
XML字符串xmlString中的值:
根節點=html
result=0
username=yangrong
title=dom4j解析一個例子
subID=36242519880716
banlce=1000
password=123456