java解析xml字符串(用dom4j)


1.package com.smsServer.Dhst;

2.

3.import java.util.HashMap;

4.import java.util.Iterator;

5.import java.util.Map;

6.

7.import org.dom4j.Document;

8.import org.dom4j.DocumentException;

9.import org.dom4j.DocumentHelper;

10.import org.dom4j.Element;

11.

12.import com.common.object.SmsSendResponseObject;

13.

14./**

15. * @description 解析xml字符串

16. */

17.public class Test {

18.

19.    public void readStringXml(String xml) {

20.        Document doc = null;

21.        try {

22.

23.            // 读取并解析XML文档

24.

25.            // SAXReader就是一个管道,用一个流的方式,把xml文件读出来

26.

27.            // SAXReader reader = new SAXReader(); //User.hbm.xml表示你要解析的xml文档

28.

29.            // Document document = reader.read(new File("User.hbm.xml"));

30.

31.            // 下面的是通过解析xml字符串的

32.

33.            doc = DocumentHelper.parseText(xml); // 将字符串转为XML

34.

35.

36.            Element rootElt = doc.getRootElement(); // 获取根节点

37.

38.            System.out.println("根节点:" + rootElt.getName()); // 拿到根节点的名称

39.

40.

41.            Iterator iter = rootElt.elementIterator("head"); // 获取根节点下的子节点head

42.

43.

44.            // 遍历head节点

45.

46.            while (iter.hasNext()) {

47.

48.                Element recordEle = (Element) iter.next();

49.                String title = recordEle.elementTextTrim("title"); // 拿到head节点下的子节点title值

50.

51.                System.out.println("title:" + title);

52.

53.                Iterator iters = recordEle.elementIterator("script"); // 获取子节点head下的子节点script

54.

55.

56.                // 遍历Header节点下的Response节点

57.

58.                while (iters.hasNext()) {

59.

60.                    Element itemEle = (Element) iters.next();

61.

62.                    String username = itemEle.elementTextTrim("username"); // 拿到head下的子节点script下的字节点username的值

63.

64.                    String password = itemEle.elementTextTrim("password");

65.

66.                    System.out.println("username:" + username);

67.                    System.out.println("password:" + password);

68.                }

69.            }

70.            Iterator iterss = rootElt.elementIterator("body"); ///获取根节点下的子节点body

71.

72.            // 遍历body节点

73.

74.            while (iterss.hasNext()) {

75.

76.                Element recordEless = (Element) iterss.next();

77.                String result = recordEless.elementTextTrim("result"); // 拿到body节点下的子节点result值

78.

79.                System.out.println("result:" + result);

80.

81.                Iterator itersElIterator = recordEless.elementIterator("form"); // 获取子节点body下的子节点form

82.

83.                // 遍历Header节点下的Response节点

84.

85.                while (itersElIterator.hasNext()) {

86.

87.                    Element itemEle = (Element) itersElIterator.next();

88.

89.                    String banlce = itemEle.elementTextTrim("banlce"); // 拿到body下的子节点form下的字节点banlce的值

90.

91.                    String subID = itemEle.elementTextTrim("subID");

92.

93.                    System.out.println("banlce:" + banlce);

94.                    System.out.println("subID:" + subID);

95.                }

96.            }

97.        } catch (DocumentException e) {

98.            e.printStackTrace();

99.

100.        } catch (Exception e) {

101.            e.printStackTrace();

102.

103.        }

104.    }

105.

106.    /**

107.     * @description 将xml字符串转换成map

108.     * @param xml

109.     * @return Map

110.     */

111.    public static Map readStringXmlOut(String xml) {

112.        Map map = new HashMap();

113.        Document doc = null;

114.        try {

115.            doc = DocumentHelper.parseText(xml); // 将字符串转为XML

116.

117.            Element rootElt = doc.getRootElement(); // 获取根节点

118.

119.            System.out.println("根节点:" + rootElt.getName()); // 拿到根节点的名称

120.

121.

122.            Iterator iter = rootElt.elementIterator("head"); // 获取根节点下的子节点head

123.

124.            // 遍历head节点

125.

126.            while (iter.hasNext()) {

127.

128.                Element recordEle = (Element) iter.next();

129.                String title = recordEle.elementTextTrim("title"); // 拿到head节点下的子节点title值

130.

131.                System.out.println("title:" + title);

132.                map.put("title", title);

133.

134.                Iterator iters = recordEle.elementIterator("script"); // 获取子节点head下的子节点script

135.

136.

137.                // 遍历Header节点下的Response节点

138.

139.                while (iters.hasNext()) {

140.

141.                    Element itemEle = (Element) iters.next();

142.

143.                    String username = itemEle.elementTextTrim("username"); // 拿到head下的子节点script下的字节点username的值

144.

145.                    String password = itemEle.elementTextTrim("password");

146.

147.                    System.out.println("username:" + username);

148.                    System.out.println("password:" + password);

149.                    map.put("username", username);

150.                    map.put("password", password);

151.

152.                }

153.            }

154.

155.            Iterator iterss = rootElt.elementIterator("body"); ///获取根节点下的子节点body

156.

157.            // 遍历body节点

158.

159.            while (iterss.hasNext()) {

160.                Element recordEless = (Element) iterss.next();

161.                String result = recordEless.elementTextTrim("result"); // 拿到body节点下的子节点result值

162.

163.                System.out.println("result:" + result);

164.

165.                Iterator itersElIterator = recordEless.elementIterator("form"); // 获取子节点body下的子节点form

166.

167.                // 遍历Header节点下的Response节点

168.

169.                while (itersElIterator.hasNext()) {

170.

171.                    Element itemEle = (Element) itersElIterator.next();

172.

173.                    String banlce = itemEle.elementTextTrim("banlce"); // 拿到body下的子节点form下的字节点banlce的值

174.

175.                    String subID = itemEle.elementTextTrim("subID");

176.

177.                    System.out.println("banlce:" + banlce);

178.                    System.out.println("subID:" + subID);

179.                    map.put("result", result);

180.                    map.put("banlce", banlce);

181.                    map.put("subID", subID);

182.                }

183.            }

184.        } catch (DocumentException e) {

185.            e.printStackTrace();

186.        } catch (Exception e) {

187.            e.printStackTrace();

188.        }

189.        return map;

190.    }

191.    public static void parse(String xml)

192.    {

193.        Document doc = null;

194.        try {

195.            doc = DocumentHelper.parseText(xml); // 将字符串转为XML

196.

197.            Element rootElt = doc.getRootElement(); // 获取根节点smsReport

198.

199.            Iterator iters = rootElt.elementIterator("sendResp"); // 获取根节点下的子节点sms

200.

201.            while (iters.hasNext()) {

202.                Element recordEle1 = (Element) iters.next();

203.                Iterator iter = recordEle1.elementIterator("sms");

204.                int i=0;

205.                // 遍历sms节点

206.

207.                while (iter.hasNext()) {

208.                    Element recordEle = (Element) iter.next();

209.                    SmsSendResponseObject r = new SmsSendResponseObject();

210.                    String phone = recordEle.elementTextTrim("phone"); // 拿到sms节点下的子节点stat值

211.

212.                    String smsID = recordEle.elementTextTrim("smsID"); // 拿到sms节点下的子节点stat值

213.

214.                    System.out.println(phone+"==="+smsID);

215.                }

216.            }

217.        } catch (DocumentException e) {

218.            e.printStackTrace();

219.        } catch (Exception e) {

220.            e.printStackTrace();

221.        }

222.    }

223.    public static void main(String[] args) {

224.

225.        // 下面是需要解析的xml字符串例子

226.

227.        String xmlString = "<html>" + "<head>" + "<title>dom4j解析一个例子</title>"

228.                + "<script>" + "<username>yangrong</username>"

229.                + "<password>123456</password>" + "</script>" + "</head>"

230.                + "<body>" + "<result>0</result>" + "<form>"

231.                + "<banlce>1000</banlce>" + "<subID>36242519880716</subID>"

232.                + "</form>" + "</body>" + "</html>";

233.

234.        /*

235.         * Test2 test = new Test2(); test.readStringXml(xmlString);

236.         */

237.        Map map = readStringXmlOut(xmlString);

238.        Iterator iters = map.keySet().iterator();

239.        while (iters.hasNext()) {

240.            String key = iters.next().toString(); // 拿到键

241.

242.            String val = map.get(key).toString(); // 拿到值

243.

244.            System.out.println(key + "=" + val);

245.        }

246.        String xml="<batchSendResp><sendResp><sms><phone>137000000</phone><smsID>ff8080813349da9001334f0eed8c5923</smsID></sms></sendResp><sendResp><sms><phone>187000000</phone><smsID>ff8080813349da9001334f0eee045924</smsID></sms></sendResp></batchSendResp>";

247.        parse(xml);

248.    }

249.

250.}





运行之后的结果会是:

根节点: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

你可能感兴趣的:(jvm)