1//先加入dom4j.jar包 2import java.util.HashMap; 3import java.util.Iterator; 4import java.util.Map; 5 6import org.dom4j.Document; 7import org.dom4j.DocumentException; 8import org.dom4j.DocumentHelper; 9import org.dom4j.Element; 10 11/** 12* @Title: TestDom4j.java 13* @Package 14* @Description: 解析xml字符串 15* @author 无处不在 16* @date 2012-11-20 下午05:14:05 17* @version V1.0 18*/ 19publicclass TestDom4j { 20 21publicvoid readStringXml(String xml) { 22 Document doc = null; 23try { 24 25// 读取并解析XML文档 26// SAXReader就是一个管道,用一个流的方式,把xml文件读出来 27// 28// SAXReader reader = new SAXReader(); //User.hbm.xml表示你要解析的xml文档 29// Document document = reader.read(new File("User.hbm.xml")); 30// 下面的是通过解析xml字符串的 31 doc = DocumentHelper.parseText(xml); // 将字符串转为XML 32 33 Element rootElt = doc.getRootElement(); // 获取根节点 34 System.out.println("根节点:" + rootElt.getName()); // 拿到根节点的名称 35 36 Iterator iter = rootElt.elementIterator("head"); // 获取根节点下的子节点head 37 38// 遍历head节点 39while (iter.hasNext()) { 40 41 Element recordEle = (Element) iter.next(); 42 String title = recordEle.elementTextTrim("title"); // 拿到head节点下的子节点title值 43 System.out.println("title:" + title); 44 45 Iterator iters = recordEle.elementIterator("script"); // 获取子节点head下的子节点script 46 47// 遍历Header节点下的Response节点 48while (iters.hasNext()) { 49 50 Element itemEle = (Element) iters.next(); 51 52 String username = itemEle.elementTextTrim("username"); // 拿到head下的子节点script下的字节点username的值 53 String password = itemEle.elementTextTrim("password"); 54 55 System.out.println("username:" + username); 56 System.out.println("password:" + password); 57 } 58 } 59 Iterator iterss = rootElt.elementIterator("body"); ///获取根节点下的子节点body 60// 遍历body节点 61while (iterss.hasNext()) { 62 63 Element recordEless = (Element) iterss.next(); 64 String result = recordEless.elementTextTrim("result"); // 拿到body节点下的子节点result值 65 System.out.println("result:" + result); 66 67 Iterator itersElIterator = recordEless.elementIterator("form"); // 获取子节点body下的子节点form 68// 遍历Header节点下的Response节点 69while (itersElIterator.hasNext()) { 70 71 Element itemEle = (Element) itersElIterator.next(); 72 73 String banlce = itemEle.elementTextTrim("banlce"); // 拿到body下的子节点form下的字节点banlce的值 74 String subID = itemEle.elementTextTrim("subID"); 75 76 System.out.println("banlce:" + banlce); 77 System.out.println("subID:" + subID); 78 } 79 } 80 } catch (DocumentException e) { 81 e.printStackTrace(); 82 83 } catch (Exception e) { 84 e.printStackTrace(); 85 86 } 87 }
/** 90 * @description 将xml字符串转换成map 91 * @param xml 92 * @return Map 93*/ 94publicstatic Map readStringXmlOut(String xml) { 95 Map map = new HashMap(); 96 Document doc = null; 97try { 98// 将字符串转为XML 99 doc = DocumentHelper.parseText(xml); 100// 获取根节点101 Element rootElt = doc.getRootElement(); 102// 拿到根节点的名称103 System.out.println("根节点:" + rootElt.getName()); 104105// 获取根节点下的子节点head106 Iterator iter = rootElt.elementIterator("head"); 107// 遍历head节点108while (iter.hasNext()) { 109110 Element recordEle = (Element) iter.next(); 111// 拿到head节点下的子节点title值112 String title = recordEle.elementTextTrim("title"); 113 System.out.println("title:" + title); 114 map.put("title", title); 115// 获取子节点head下的子节点script116 Iterator iters = recordEle.elementIterator("script"); 117// 遍历Header节点下的Response节点118while (iters.hasNext()) { 119 Element itemEle = (Element) iters.next(); 120// 拿到head下的子节点script下的字节点username的值121 String username = itemEle.elementTextTrim("username"); 122 String password = itemEle.elementTextTrim("password"); 123124 System.out.println("username:" + username); 125 System.out.println("password:" + password); 126 map.put("username", username); 127 map.put("password", password); 128 } 129 } 130131//获取根节点下的子节点body132 Iterator iterss = rootElt.elementIterator("body"); 133// 遍历body节点134while (iterss.hasNext()) { 135 Element recordEless = (Element) iterss.next(); 136// 拿到body节点下的子节点result值137 String result = recordEless.elementTextTrim("result"); 138 System.out.println("result:" + result); 139// 获取子节点body下的子节点form140 Iterator itersElIterator = recordEless.elementIterator("form"); 141// 遍历Header节点下的Response节点142while (itersElIterator.hasNext()) { 143 Element itemEle = (Element) itersElIterator.next(); 144// 拿到body下的子节点form下的字节点banlce的值145 String banlce = itemEle.elementTextTrim("banlce"); 146 String subID = itemEle.elementTextTrim("subID"); 147148 System.out.println("banlce:" + banlce); 149 System.out.println("subID:" + subID); 150 map.put("result", result); 151 map.put("banlce", banlce); 152 map.put("subID", subID); 153 } 154 } 155 } catch (DocumentException e) { 156 e.printStackTrace(); 157 } catch (Exception e) { 158 e.printStackTrace(); 159 } 160return map; 161 } 162
publicstaticvoid main(String[] args) { 164165// 下面是需要解析的xml字符串例子166 String xmlString = "<html>" + "<head>" + "<title>dom4j解析一个例子</title>" 167 + "<script>" + "<username>yangrong</username>" 168 + "<password>123456</password>" + "</script>" + "</head>" 169 + "<body>" + "<result>0</result>" + "<form>" 170 + "<banlce>1000</banlce>" + "<subID>36242519880716</subID>" 171 + "</form>" + "</body>" + "</html>"; 172173/*174 * Test2 test = new Test2(); test.readStringXml(xmlString); 175*/176 Map map = readStringXmlOut(xmlString); 177 Iterator iters = map.keySet().iterator(); 178while (iters.hasNext()) { 179 String key = iters.next().toString(); // 拿到键180 String val = map.get(key).toString(); // 拿到值181 System.out.println(key + "=" + val); 182 } 183 } 184185 }
时间: 2024-10-10 07:17:14