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