java解析xml字符串(用dom4j)
- 1、xml文档或节点转换为字符串
- //xml文档或节点转换为字符串
- @Test
- public void test5()throws Exception{
- //创建SAXReader对象
- SAXReader reader = new SAXReader();
- //读取文件 转换成Document
- Document document = reader.read(new File("src/cn/com/yy/dom4j/s.xml"));
- //document转换为String字符串
- String documentStr = document.asXML();
- System.out.println("document 字符串:" + documentStr);
- //获取根节点
- Element root = document.getRootElement();
- //根节点转换为String字符串
- String rootStr = root.asXML();
- System.out.println("root 字符串:" + rootStr);
- //获取其中student1节点
- Element student1Node = root.element("student1");
- //student1节点转换为String字符串
- String student1Str = student1Node.asXML();
- System.out.println("student1 字符串:" + student1Str);
- package com.smsServer.Dhst;
- 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;
- import com.common.object.SmsSendResponseObject;
- /**
- * @description 解析xml字符串
- */
- public class Test {
- 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 {
- 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);
- map.put("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);
- map.put("username", username);
- map.put("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);
- 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 parse(String xml)
- {
- Document doc = null;
- try {
- doc = DocumentHelper.parseText(xml); // 将字符串转为XML
- Element rootElt = doc.getRootElement(); // 获取根节点smsReport
- Iterator iters = rootElt.elementIterator("sendResp"); // 获取根节点下的子节点sms
- while (iters.hasNext()) {
- Element recordEle1 = (Element) iters.next();
- Iterator iter = recordEle1.elementIterator("sms");
- int i=0;
- // 遍历sms节点
- while (iter.hasNext()) {
- Element recordEle = (Element) iter.next();
- SmsSendResponseObject r = new SmsSendResponseObject();
- String phone = recordEle.elementTextTrim("phone"); // 拿到sms节点下的子节点stat值
- String smsID = recordEle.elementTextTrim("smsID"); // 拿到sms节点下的子节点stat值
- System.out.println(phone+"==="+smsID);
- }
- }
- } catch (DocumentException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- // 下面是需要解析的xml字符串例子
- String xmlString = "" + "" + ""
- + "" + ""
- + "" + "0" + ""
- + "1000" + "36242519880716"
- + "" + " " + "";
- /*
- * 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);
- }
- String xml="137000000ff8080813349da9001334f0eed8c5923187000000ff8080813349da9001334f0eee045924";
- parse(xml);
- }
- }
运行之后的结果会是:
根节点: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
相关推荐
donghedonghe 2013-05-31
tdeclipse 2011-02-28
pigsmall 2020-11-19
SXIAOYI 2020-09-16
Ladyseven 2020-07-25
whileinsist 2020-06-24
gufudhn 2020-06-12
冰蝶 2020-06-05
LinuxAndroidAI 2020-06-04
supperme 2020-05-28
yaodilu 2020-05-10
e度空间 2020-04-27
云端漂移 2020-04-09
peterwzc 2020-03-17
有心就有方向 2012-09-03
ebuild 2013-05-14
linuxprobe0 2013-04-15
linuxprobe0 2013-04-01