java解析xml字符串(用dom4j)

  1. 1、xml文档或节点转换为字符串
  1. //xml文档或节点转换为字符串  
  2.     @Test  
  3.     public void test5()throws Exception{  
  4.         //创建SAXReader对象  
  5.         SAXReader reader = new SAXReader();  
  6.         //读取文件 转换成Document  
  7.         Document document = reader.read(new File("src/cn/com/yy/dom4j/s.xml"));  
  8.         //document转换为String字符串  
  9.         String documentStr = document.asXML();  
  10.         System.out.println("document 字符串:" + documentStr);  
  11.         //获取根节点  
  12.         Element root = document.getRootElement();  
  13.         //根节点转换为String字符串  
  14.         String rootStr = root.asXML();  
  15.         System.out.println("root 字符串:" + rootStr);  
  16.         //获取其中student1节点  
  17.         Element student1Node = root.element("student1");  
  18.         //student1节点转换为String字符串  
  19.         String student1Str = student1Node.asXML();  
  20.         System.out.println("student1 字符串:" + student1Str); 
  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 = "" + "" + ""
  149.                 + "" + ""
  150.                 + "" + "0" + ""
  151.                 + "1000" + "36242519880716"
  152.                 + "" + " " + "";
  153.         /*
  154.          * Test2 test = new Test2(); test.readStringXml(xmlString);
  155.          */
  156.         Map map = readStringXmlOut(xmlString);
  157.         Iterator iters = map.keySet().iterator();
  158.         while (iters.hasNext()) {
  159.             String key = iters.next().toString(); // 拿到键
  160.             String val = map.get(key).toString(); // 拿到值
  161.             System.out.println(key + "=" + val);
  162.         }
  163.         String xml="137000000ff8080813349da9001334f0eed8c5923187000000ff8080813349da9001334f0eee045924";
  164.         parse(xml);
  165.     }
  166. }

运行之后的结果会是:

根节点:html
title:dom4j解析一个例子
username:yangrong
password:123456
result:0
banlce:1000
subID:36242519880716
result=0
username=yangrong
title=dom4j解析一个例子
subID=36242519880716
banlce=1000
password=123456
137000000===ff8080813349da9001334f0eed8c5923
187000000===ff8080813349da9001334f0eee045924

相关推荐