java转成xml
2021-01-26 22:13
标签:turn col pre sse put ubi VID cms rip java转成xml 标签:turn col pre sse put ubi VID cms rip 原文地址:https://www.cnblogs.com/fenghh/p/12850756.html 1 package com.chinamcloud.cms.article.util;
2
3 import com.chinamcloud.cms.article.dto.ZjyArticleDto;
4 import com.google.common.collect.Maps;
5 import org.assertj.core.util.Lists;
6 import org.dom4j.Document;
7 import org.dom4j.DocumentException;
8 import org.dom4j.DocumentHelper;
9 import org.dom4j.Element;
10
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Map;
14
18 public class XmlUtil {
19
20 public static void readStringXml(String xml) {
21 Document doc = null;
22 try {
23 doc = DocumentHelper.parseText(xml); // 将字符串转为XML
24 Element rootElt = doc.getRootElement(); // 获取根节点
25 System.out.println("根节点:" + rootElt.getName()); // 拿到根节点的名称
26 Iterator iter = rootElt.elementIterator("head"); // 获取根节点下的子节点head
27 // 遍历head节点
28 while (iter.hasNext()) {
29 Element recordEle = (Element) iter.next();
30 String title = recordEle.elementTextTrim("title"); // 拿到head节点下的子节点title值
31 System.out.println("title:" + title);
32 Iterator iters = recordEle.elementIterator("script"); // 获取子节点head下的子节点script
33 // 遍历Header节点下的Response节点
34 while (iters.hasNext()) {
35 Element itemEle = (Element) iters.next();
36 String username = itemEle.elementTextTrim("username"); // 拿到head下的子节点script下的字节点username的值
37 String password = itemEle.elementTextTrim("password");
38 System.out.println("username:" + username);
39 System.out.println("password:" + password);
40 }
41 }
42 Iterator iterss = rootElt.elementIterator("body"); ///获取根节点下的子节点body
43 // 遍历body节点
44 while (iterss.hasNext()) {
45 Element recordEless = (Element) iterss.next();
46 String result = recordEless.elementTextTrim("result"); // 拿到body节点下的子节点result值
47 System.out.println("result:" + result);
48 Iterator itersElIterator = recordEless.elementIterator("form"); // 获取子节点body下的子节点form
49 // 遍历Header节点下的Response节点
50 while (itersElIterator.hasNext()) {
51 Element itemEle = (Element) itersElIterator.next();
52 String banlce = itemEle.elementTextTrim("banlce"); // 拿到body下的子节点form下的字节点banlce的值
53 String subID = itemEle.elementTextTrim("subID");
54 System.out.println("banlce:" + banlce);
55 System.out.println("subID:" + subID);
56 }
57 }
58 } catch (DocumentException e) {
59 e.printStackTrace();
60 } catch (Exception e) {
61 e.printStackTrace();
62 }
63 }
64
65 public static Map
上一篇:JAVA——赋值指派中的别名现象