java 解析xml字符串用dom4j

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。