package test; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; /** * @Title: TestDom4j.java * @Package * @Description: 解析xml字符串 * @author 無處不在 * @date 2012-11-20 下午05:14:05 * @version V1.0 */ public class ParseDataUtil { public void readStringXml(String xml) { Document doc = null; try { // 讀取並解析XML文檔 // SAXReader就是一個管道,用一個流的方式,把xml文件讀出來 // // SAXReader reader = new SAXReader(); //User.hbm.xml表示你要解析的xml文檔 // Document document = reader.read(new File("User.hbm.xml")); // 下面的是經過解析xml字符串的 doc = DocumentHelper.parseText(xml); // 將字符串轉爲XML Element rootElt = doc.getRootElement(); // 獲取根節點 System.out.println("根節點:" + rootElt.getName()); // 拿到根節點的名稱 Iterator iter = rootElt.elementIterator("head"); // 獲取根節點下的子節點head // 遍歷head節點 while (iter.hasNext()) { Element recordEle = (Element) iter.next(); String title = recordEle.elementTextTrim("title"); // 拿到head節點下的子節點title值 System.out.println("title:" + title); Iterator iters = recordEle.elementIterator("script"); // 獲取子節點head下的子節點script // 遍歷Header節點下的Response節點 while (iters.hasNext()) { Element itemEle = (Element) iters.next(); String username = itemEle.elementTextTrim("username"); // 拿到head下的子節點script下的字節點username的值 String password = itemEle.elementTextTrim("password"); System.out.println("username:" + username); System.out.println("password:" + password); } } Iterator iterss = rootElt.elementIterator("body"); ///獲取根節點下的子節點body // 遍歷body節點 while (iterss.hasNext()) { Element recordEless = (Element) iterss.next(); String result = recordEless.elementTextTrim("result"); // 拿到body節點下的子節點result值 System.out.println("result:" + result); Iterator itersElIterator = recordEless.elementIterator("form"); // 獲取子節點body下的子節點form // 遍歷Header節點下的Response節點 while (itersElIterator.hasNext()) { Element itemEle = (Element) itersElIterator.next(); String banlce = itemEle.elementTextTrim("banlce"); // 拿到body下的子節點form下的字節點banlce的值 String subID = itemEle.elementTextTrim("subID"); System.out.println("banlce:" + banlce); System.out.println("subID:" + subID); } } } catch (DocumentException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * @description 將xml字符串轉換成map * @param xml * @return Map */ public static Map readStringXmlOut(String xml) { Map map = new HashMap(); Document doc = null; try { // 將字符串轉爲XML doc = DocumentHelper.parseText(xml); // 獲取根節點 Element rootElt = doc.getRootElement(); // 拿到根節點的名稱 System.out.println("根節點:" + rootElt.getName()); // 獲取根節點下的子節點head Iterator iter = rootElt.elementIterator("head"); // 遍歷head節點 while (iter.hasNext()) { Element recordEle = (Element) iter.next(); // 拿到head節點下的子節點title值 String title = recordEle.elementTextTrim("title"); System.out.println("title:" + title); map.put("title", title); // 獲取子節點head下的子節點script Iterator iters = recordEle.elementIterator("script"); // 遍歷Header節點下的Response節點 while (iters.hasNext()) { Element itemEle = (Element) iters.next(); // 拿到head下的子節點script下的字節點username的值 String username = itemEle.elementTextTrim("username"); String password = itemEle.elementTextTrim("password"); System.out.println("username:" + username); System.out.println("password:" + password); map.put("username", username); map.put("password", password); } } //獲取根節點下的子節點body Iterator iterss = rootElt.elementIterator("body"); // 遍歷body節點 while (iterss.hasNext()) { Element recordEless = (Element) iterss.next(); // 拿到body節點下的子節點result值 String result = recordEless.elementTextTrim("result"); System.out.println("result:" + result); // 獲取子節點body下的子節點form Iterator itersElIterator = recordEless.elementIterator("form"); // 遍歷Header節點下的Response節點 while (itersElIterator.hasNext()) { Element itemEle = (Element) itersElIterator.next(); // 拿到body下的子節點form下的字節點banlce的值 String banlce = itemEle.elementTextTrim("banlce"); String subID = itemEle.elementTextTrim("subID"); System.out.println("banlce:" + banlce); System.out.println("subID:" + subID); map.put("result", result); map.put("banlce", banlce); map.put("subID", subID); } } } catch (DocumentException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return map; } public static void main(String[] args) { // 下面是須要解析的xml字符串例子 String xmlString = "<html>" + "<head>" + "<title>dom4j解析一個例子</title>"+ "<script>" + "<username>yangrong</username>"+ "<password>123456</password>" + "</script>" + "</head>"+ "<body>" + "<result>0</result>" + "<form>" + "<banlce>1000</banlce>" + "<subID>36242519880716</subID>"+ "</form>" + "</body>" + "</html>"; /* * Test2 test = new Test2(); test.readStringXml(xmlString); */ Map map = readStringXmlOut(xmlString); Iterator iters = map.keySet().iterator(); while (iters.hasNext()) { String key = iters.next().toString(); // 拿到鍵 String val = map.get(key).toString(); // 拿到值 System.out.println(key + "=" + val); } } }