传过来一个xml文件,需要转换成Map,能够应对不用结构的xml,而不是只针对固定格式的xml.
转换规则: 1.主要是Map与List的互相嵌套 2.同名称的节点会被装进List 如下面的xml文件:1 23 2 4test@corp.gm.com 5tom 6XML-未来世界 Message 7xml-tom 8xml-test@163.com 9xml-gm 10xml-hello guys 1112 2713 20http://china.gm.com 14gm中文站 15gm中文站desc 16gm中文站desc2 17gm中文站desc3 18gm中文站desc4 19products 2122 26http://www.gm.com 23gm 24gmdesc 2528 5529 4030 http://china.gm.com/pics/com.cn.gm/story/frsc.jpg31 32黄晓豫 3334 39attach-desc1 35attach-desc2 36attach-desc3 37attach-desc4 3841 5442 http://china.gm.com/pics/com.cn.gm/story/wr.jpg43 44张中汉 4546 49attach-desc1 47attach-desc2 4850 53attach-desc1 51attach-desc2 5256 62desc_item_1 57desc_item_2 58desc_item_3 59desc_item_4 60desc_item_5 61
转换后的的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 MapDom2Map(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 }