xml 转map dom4j
生活随笔
收集整理的這篇文章主要介紹了
xml 转map dom4j
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://ziyu-1.iteye.com/blog/469003
?
傳過來一個xml文件,需要轉換成Map,能夠應對不用結構的xml,而不是只針對固定格式的xml.?
轉換規則:?
1.主要是Map與List的互相嵌套?
2.同名稱的節點會被裝進List?
如下面的xml文件:?
?
轉換后的的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:?
下面是一個創建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 args 11 */ 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 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 }?
轉載于:https://www.cnblogs.com/a757956132/p/5566464.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的xml 转map dom4j的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MVC系列-6.注册页面
- 下一篇: 学生选课数据库SQL语句练习题——成绩表