把xml转换成map

传过来一个xml文件,需要转换成Map,能够应对不用结构的xml,而不是只针对固定格式的xml. 
转换规则: 
1.主要是Map与List的互相嵌套 
2.同名称的节点会被装进List 

如下面的xml文件: 
Xml代码   收藏代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <GmMail>  
  3.     <template_id>2template_id>  
  4.     <to_address>[email protected]to_address>  
  5.     <to_alias>tomto_alias>  
  6.     <message>XML-未来世界 Messagemessage>  
  7.     <recipient_name>xml-tomrecipient_name>  
  8.     <send_address>[email protected]send_address>  
  9.     <sender_comp_name>xml-gmsender_comp_name>  
  10.     <subject>xml-hello guyssubject>  
  11.     <selected_items>  
  12.         <item_group>  
  13.             <item_url>http://china.gm.comitem_url>  
  14.             <item_name>gm中文站item_name>  
  15.             <item_desc>gm中文站descitem_desc>  
  16.             <item_desc2>gm中文站desc2item_desc2>  
  17.             <item_desc3>gm中文站desc3item_desc3>  
  18.             <item_desc4>gm中文站desc4item_desc4>  
  19.         item_group>  
  20.         <items_source>productsitems_source>  
  21.         <item_group>  
  22.             <item_url>http://www.gm.comitem_url>  
  23.             <item_name>gmitem_name>  
  24.             <item_desc>gmdescitem_desc>  
  25.         item_group>  
  26.     selected_items>  
  27.     <attachments>  
  28.         <attachment-group>  
  29.             <attachment_url>  
  30.                 http://china.gm.com/pics/com.cn.gm/story/frsc.jpg  
  31.             attachment_url>  
  32.             <attachment_name>黄晓豫attachment_name>  
  33.             <attachment_desc>  
  34.                 <desc1>attach-desc1desc1>  
  35.                 <desc2>attach-desc2desc2>  
  36.                 <desc3>attach-desc3desc3>  
  37.                 <desc4>attach-desc4desc4>  
  38.             attachment_desc>  
  39.         attachment-group>  
  40.         <attachment-group>  
  41.             <attachment_url>  
  42.                 http://china.gm.com/pics/com.cn.gm/story/wr.jpg  
  43.             attachment_url>  
  44.             <attachment_name>张中汉attachment_name>  
  45.             <attachment_desc>  
  46.                 <desc1>attach-desc1desc1>  
  47.                 <desc2>attach-desc2desc2>  
  48.             attachment_desc>  
  49.             <attachment_desc>  
  50.                 <desc1>attach-desc1desc1>  
  51.                 <desc2>attach-desc2desc2>  
  52.             attachment_desc>  
  53.         attachment-group>  
  54.     attachments>  
  55.     <desc_group>  
  56.         <desc_item>desc_item_1desc_item>  
  57.         <desc_item>desc_item_2desc_item>  
  58.         <desc_item>desc_item_3desc_item>  
  59.         <desc_item>desc_item_4desc_item>  
  60.         <desc_item>desc_item_5desc_item>  
  61.     desc_group>  
  62. GmMail>  


转换后的的Map(转换成字符串的效果): 
Java代码   收藏代码
  1. {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, [email protected], 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, [email protected]}  


xml处理用的dom4j 

下面是实现转换的主要类XmlUtils:  
Java代码   收藏代码
  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.             }else  
  26.                 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.                     }else  
  55.                         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.                     }else  
  71.                         map.put(iter.getName(), iter.getText());  
  72.                 }  
  73.             }  
  74.         }else  
  75.             map.put(e.getName(), e.getText());  
  76.         return map;  
  77.     }  
  78.   
  79. }  


下面是一个创建xml的类(不重要,可以忽略,只是用来生成一个xml以便测试,可以自己写) 
Java代码   收藏代码
  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("[email protected]");  
  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("[email protected]");  
  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调用类: 
Java代码   收藏代码
  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://ziyu-1.iteye.com/blog/469003

你可能感兴趣的:(后台知识)