首页 > 代码库 > 使用Dom4j解析xml字符串

使用Dom4j解析xml字符串

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>
hmb.xml

解析代码:

  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           //获取根节点下的子节点body112           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下的子节点form119               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中的值:根节点:htmltitle:dom4j解析一个例子username:yangrongpassword:123456result:0banlce:1000subID:36242519880716XML字符串xmlString中的值:根节点=htmlresult=0username=yangrongtitle=dom4j解析一个例子subID=36242519880716banlce=1000password=123456

 

使用Dom4j解析xml字符串