博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xml 转map dom4j
阅读量:6379 次
发布时间:2019-06-23

本文共 11008 字,大约阅读时间需要 36 分钟。

 

传过来一个xml文件,需要转换成Map,能够应对不用结构的xml,而不是只针对固定格式的xml. 

转换规则: 
1.主要是Map与List的互相嵌套 
2.同名称的节点会被装进List 
如下面的xml文件: 

1 
2
3
2
4
test@corp.gm.com
5
tom
6
XML-未来世界 Message
7
xml-tom
8
xml-test@163.com
9
xml-gm
10
xml-hello guys
11
12
13
http://china.gm.com
14
gm中文站
15
gm中文站desc
16
gm中文站desc2
17
gm中文站desc3
18
gm中文站desc4
19
20
products
21
22
http://www.gm.com
23
gm
24
gmdesc
25
26
27
28
29
30 http://china.gm.com/pics/com.cn.gm/story/frsc.jpg31
32
黄晓豫
33
34
attach-desc1
35
attach-desc2
36
attach-desc3
37
attach-desc4
38
39
40
41
42 http://china.gm.com/pics/com.cn.gm/story/wr.jpg43
44
张中汉
45
46
attach-desc1
47
attach-desc2
48
49
50
attach-desc1
51
attach-desc2
52
53
54
55
56
desc_item_1
57
desc_item_2
58
desc_item_3
59
desc_item_4
60
desc_item_5
61
62

 

转换后的的Map(转换成字符串的效果): 

{message=XML-未来世界 Message, selected_items={items_source=products, item_group=[{item_name=gm中文站, item_desc=gm中文站desc, item_desc2=gm中文站desc2, item_desc4=gm中文站desc4, item_url=http://china.gm.com, item_desc3=gm中文站desc3}, {item_name=gm, item_desc=gmdesc, item_url=http://www.gm.com}]}, to_alias=tom, desc_group={desc_item=[desc_item_1, desc_item_2, desc_item_3, desc_item_4, desc_item_5]}, sender_comp_name=xml-gm, subject=xml-hello guys, to_address=test@corp.gm.com, template_id=2, attachments={attachment-group=[{attachment_url=http://china.gm.com/pics/com.cn.gm/story/frsc.jpg, attachment_name=黄晓豫, attachment_desc={desc3=attach-desc3, desc4=attach-desc4, desc1=attach-desc1, desc2=attach-desc2}}, {attachment_url=http://china.gm.com/pics/com.cn.gm/story/wr.jpg, attachment_name=张中汉, attachment_desc=[{desc1=attach-desc1, desc2=attach-desc2}, {desc1=attach-desc1, desc2=attach-desc2}]}]}, recipient_name=xml-tom, send_address=xml-test@163.com}

xml处理用的dom4j 

下面是实现转换的主要类XmlUtils: 

1 package xml.dom4j; 2  3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.List; 7 import java.util.Map; 8  9 import org.dom4j.Document;10 import org.dom4j.Element;11 12 public class XmlUtils {13     14     public static Map
Dom2Map(Document doc){15 Map
map = new HashMap
();16 if(doc == null)17 return map;18 Element root = doc.getRootElement();19 for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {20 Element e = (Element) iterator.next();21 //System.out.println(e.getName());22 List list = e.elements();23 if(list.size() > 0){24 map.put(e.getName(), Dom2Map(e));25 }else26 map.put(e.getName(), e.getText());27 }28 return map;29 }30 31 32 public static Map Dom2Map(Element e){33 Map map = new HashMap();34 List list = e.elements();35 if(list.size() > 0){36 for (int i = 0;i < list.size(); i++) {37 Element iter = (Element) list.get(i);38 List mapList = new ArrayList();39 40 if(iter.elements().size() > 0){41 Map m = Dom2Map(iter);42 if(map.get(iter.getName()) != null){43 Object obj = map.get(iter.getName());44 if(!obj.getClass().getName().equals("java.util.ArrayList")){45 mapList = new ArrayList();46 mapList.add(obj);47 mapList.add(m);48 }49 if(obj.getClass().getName().equals("java.util.ArrayList")){50 mapList = (List) obj;51 mapList.add(m);52 }53 map.put(iter.getName(), mapList);54 }else55 map.put(iter.getName(), m);56 }57 else{58 if(map.get(iter.getName()) != null){59 Object obj = map.get(iter.getName());60 if(!obj.getClass().getName().equals("java.util.ArrayList")){61 mapList = new ArrayList();62 mapList.add(obj);63 mapList.add(iter.getText());64 }65 if(obj.getClass().getName().equals("java.util.ArrayList")){66 mapList = (List) obj;67 mapList.add(iter.getText());68 }69 map.put(iter.getName(), mapList);70 }else71 map.put(iter.getName(), iter.getText());72 }73 }74 }else75 map.put(e.getName(), e.getText());76 return map;77 }78 79 }

下面是一个创建xml的类(不重要,可以忽略,只是用来生成一个xml以便测试,可以自己写) 

1 package xml.dom4j; 2  3 import org.dom4j.Document; 4 import org.dom4j.DocumentHelper; 5 import org.dom4j.Element; 6  7 public class CreateXml { 8  9     /**10      * @param args11      */12     public static void main(String[] args) {13         // TODO Auto-generated method stub14 15     }16     17     public Document createXml(){18         Document doc = DocumentHelper.createDocument();19         Element e = doc.addElement("GmMail");20         e.addElement("template_id").setText("2");21         e.addElement("to_address").setText("test@corp.gm.com");22         e.addElement("to_alias").setText("tom");23         e.addElement("message").setText("XML-未来世界 Message");24         e.addElement("recipient_name").setText("xml-tom");25         e.addElement("send_address").setText("xml-test@163.com");26         e.addElement("sender_comp_name").setText("xml-gm");27         e.addElement("subject").setText("xml-hello guys");28         Element Selecteditems = e.addElement("selected_items");29             30                 Element itemsGroup = Selecteditems.addElement("item_group");31             //Element item = itemsGroup.addElement("item");32                     itemsGroup.addElement("item_url").setText("http://china.gm.com");33                     itemsGroup.addElement("item_name").setText("gm中文站");34                     itemsGroup.addElement("item_desc").setText("gm中文站desc");35                     itemsGroup.addElement("item_desc2").setText("gm中文站desc2");36                     itemsGroup.addElement("item_desc3").setText("gm中文站desc3");37                     itemsGroup.addElement("item_desc4").setText("gm中文站desc4");38                 Selecteditems.addElement("items_source").setText("products");39                 itemsGroup = Selecteditems.addElement("item_group");40                     itemsGroup.addElement("item_url").setText("http://www.gm.com");41                     itemsGroup.addElement("item_name").setText("gm");42                     itemsGroup.addElement("item_desc").setText("gmdesc");43         Element attachment = e.addElement("attachments");44             Element attachmentGroup =    attachment.addElement("attachment-group");45                 attachmentGroup.addElement("attachment_url").setText("http://china.gm.com/pics/com.cn.gm/story/frsc.jpg");46                 attachmentGroup.addElement("attachment_name").setText("黄晓豫");47                     Element attachmentDesc = attachmentGroup.addElement("attachment_desc");48                     attachmentDesc.addElement("desc1").setText("attach-desc1");49                     attachmentDesc.addElement("desc2").setText("attach-desc2");50                     attachmentDesc.addElement("desc3").setText("attach-desc3");51                     attachmentDesc.addElement("desc4").setText("attach-desc4");52             attachmentGroup =    attachment.addElement("attachment-group");53                 attachmentGroup.addElement("attachment_url").setText("http://china.gm.com/pics/com.cn.gm/story/wr.jpg");54                 attachmentGroup.addElement("attachment_name").setText("张中汉");55                     attachmentDesc = attachmentGroup.addElement("attachment_desc");56                         attachmentDesc.addElement("desc1").setText("attach-desc1");57                         attachmentDesc.addElement("desc2").setText("attach-desc2");58                     attachmentDesc = attachmentGroup.addElement("attachment_desc");59                         attachmentDesc.addElement("desc1").setText("attach-desc1");60                         attachmentDesc.addElement("desc2").setText("attach-desc2");61         Element descGroup = e.addElement("desc_group");62             descGroup.addElement("desc_item").setText("desc_item_1");63             descGroup.addElement("desc_item").setText("desc_item_2");64             descGroup.addElement("desc_item").setText("desc_item_3");65             descGroup.addElement("desc_item").setText("desc_item_4");66             descGroup.addElement("desc_item").setText("desc_item_5");67         return doc;68     }69 }

下面是main调用类: 

1 package xml.dom4j; 2  3 import java.util.Map; 4  5 import org.dom4j.Document; 6  7 public class Demo { 8      9     public static void main(String[] args){10         CreateXml create = new CreateXml();11         Document doc = create.createXml();12         String strXml = doc.asXML();13         System.out.println(strXml);14         15         Map map = XmlUtils.Dom2Map(doc);16         17         System.out.println(map.toString());18     }19 20 }

 

转载地址:http://bqqqa.baihongyu.com/

你可能感兴趣的文章
【转】以过来人的身份聊聊实习招聘、秋招、春招(给应届毕业生)
查看>>
英文论文润色的问题
查看>>
myeclipse异常关闭导致tomcat无法启动如何解决
查看>>
LeetCode 265: Paint House II
查看>>
matlab-调用摄像头人脸识别
查看>>
Proud Merchants详细解答
查看>>
笔记本建立wifi热点的实用详细步骤
查看>>
matlab使用常犯的错误
查看>>
Go语言的big包实现大整数运算
查看>>
Graphviz样例之无向图
查看>>
CCF201609试题
查看>>
CCF201403-1 相反数(解法二)(100分)
查看>>
Python软件目录结构
查看>>
C#之运算符重载
查看>>
SharePoint 2013 实战碎嘴(ECMAScript客户端对象模型): 提示某个列表不存在
查看>>
4.Heredoc结构形式
查看>>
python socket网络编程
查看>>
Daily Scrum9 11.13
查看>>
C语言学习笔记(一)_hello world
查看>>
软件质量
查看>>