传过来一个xml文件,需要转换成Map,能够应对不用结构的xml,而不是只针对固定格式的xml.
转换规则:
1.主要是Map与List的互相嵌套
2.同名称的节点会被装进List
如下面的xml文件:
转换后的的Map(转换成字符串的效果):
xml处理用的dom4j
下面是实现转换的主要类XmlUtils:
下面是一个创建xml的类(不重要,可以忽略,只是用来生成一个xml以便测试,可以自己写)
下面是main调用类:
转换规则:
1.主要是Map与List的互相嵌套
2.同名称的节点会被装进List
如下面的xml文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <GmMail>
- <template_id>2</template_id>
- <to_address>test@corp.gm.com</to_address>
- <to_alias>tom</to_alias>
- <message>XML-未来世界 Message</message>
- <recipient_name>xml-tom</recipient_name>
- <send_address>xml-test@163.com</send_address>
- <sender_comp_name>xml-gm</sender_comp_name>
- <subject>xml-hello guys</subject>
- <selected_items>
- <item_group>
- <item_url>http://china.gm.com</item_url>
- <item_name>gm中文站</item_name>
- <item_desc>gm中文站desc</item_desc>
- <item_desc2>gm中文站desc2</item_desc2>
- <item_desc3>gm中文站desc3</item_desc3>
- <item_desc4>gm中文站desc4</item_desc4>
- </item_group>
- <items_source>products</items_source>
- <item_group>
- <item_url>http://www.gm.com</item_url>
- <item_name>gm</item_name>
- <item_desc>gmdesc</item_desc>
- </item_group>
- </selected_items>
- <attachments>
- <attachment-group>
- <attachment_url>
- http://china.gm.com/pics/com.cn.gm/story/frsc.jpg
- </attachment_url>
- <attachment_name>黄晓豫</attachment_name>
- <attachment_desc>
- <desc1>attach-desc1</desc1>
- <desc2>attach-desc2</desc2>
- <desc3>attach-desc3</desc3>
- <desc4>attach-desc4</desc4>
- </attachment_desc>
- </attachment-group>
- <attachment-group>
- <attachment_url>
- http://china.gm.com/pics/com.cn.gm/story/wr.jpg
- </attachment_url>
- <attachment_name>张中汉</attachment_name>
- <attachment_desc>
- <desc1>attach-desc1</desc1>
- <desc2>attach-desc2</desc2>
- </attachment_desc>
- <attachment_desc>
- <desc1>attach-desc1</desc1>
- <desc2>attach-desc2</desc2>
- </attachment_desc>
- </attachment-group>
- </attachments>
- <desc_group>
- <desc_item>desc_item_1</desc_item>
- <desc_item>desc_item_2</desc_item>
- <desc_item>desc_item_3</desc_item>
- <desc_item>desc_item_4</desc_item>
- <desc_item>desc_item_5</desc_item>
- </desc_group>
- </GmMail>
转换后的的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:
- package xml.dom4j;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import org.dom4j.Document;
- import org.dom4j.Element;
- public class XmlUtils {
- public static Map<String, Object> Dom2Map(Document doc){
- Map<String, Object> map = new HashMap<String, Object>();
- if(doc == null)
- return map;
- Element root = doc.getRootElement();
- for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
- Element e = (Element) iterator.next();
- //System.out.println(e.getName());
- List list = e.elements();
- if(list.size() > 0){
- map.put(e.getName(), Dom2Map(e));
- }else
- map.put(e.getName(), e.getText());
- }
- return map;
- }
- public static Map Dom2Map(Element e){
- Map map = new HashMap();
- List list = e.elements();
- if(list.size() > 0){
- for (int i = 0;i < list.size(); i++) {
- Element iter = (Element) list.get(i);
- List mapList = new ArrayList();
- if(iter.elements().size() > 0){
- Map m = Dom2Map(iter);
- if(map.get(iter.getName()) != null){
- Object obj = map.get(iter.getName());
- if(!obj.getClass().getName().equals("java.util.ArrayList")){
- mapList = new ArrayList();
- mapList.add(obj);
- mapList.add(m);
- }
- if(obj.getClass().getName().equals("java.util.ArrayList")){
- mapList = (List) obj;
- mapList.add(m);
- }
- map.put(iter.getName(), mapList);
- }else
- map.put(iter.getName(), m);
- }
- else{
- if(map.get(iter.getName()) != null){
- Object obj = map.get(iter.getName());
- if(!obj.getClass().getName().equals("java.util.ArrayList")){
- mapList = new ArrayList();
- mapList.add(obj);
- mapList.add(iter.getText());
- }
- if(obj.getClass().getName().equals("java.util.ArrayList")){
- mapList = (List) obj;
- mapList.add(iter.getText());
- }
- map.put(iter.getName(), mapList);
- }else
- map.put(iter.getName(), iter.getText());
- }
- }
- }else
- map.put(e.getName(), e.getText());
- return map;
- }
- }
下面是一个创建xml的类(不重要,可以忽略,只是用来生成一个xml以便测试,可以自己写)
- package xml.dom4j;
- import org.dom4j.Document;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- public class CreateXml {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- }
- public Document createXml(){
- Document doc = DocumentHelper.createDocument();
- Element e = doc.addElement("GmMail");
- e.addElement("template_id").setText("2");
- e.addElement("to_address").setText("test@corp.gm.com");
- e.addElement("to_alias").setText("tom");
- e.addElement("message").setText("XML-未来世界 Message");
- e.addElement("recipient_name").setText("xml-tom");
- e.addElement("send_address").setText("xml-test@163.com");
- e.addElement("sender_comp_name").setText("xml-gm");
- e.addElement("subject").setText("xml-hello guys");
- Element Selecteditems = e.addElement("selected_items");
- Element itemsGroup = Selecteditems.addElement("item_group");
- //Element item = itemsGroup.addElement("item");
- itemsGroup.addElement("item_url").setText("http://china.gm.com");
- itemsGroup.addElement("item_name").setText("gm中文站");
- itemsGroup.addElement("item_desc").setText("gm中文站desc");
- itemsGroup.addElement("item_desc2").setText("gm中文站desc2");
- itemsGroup.addElement("item_desc3").setText("gm中文站desc3");
- itemsGroup.addElement("item_desc4").setText("gm中文站desc4");
- Selecteditems.addElement("items_source").setText("products");
- itemsGroup = Selecteditems.addElement("item_group");
- itemsGroup.addElement("item_url").setText("http://www.gm.com");
- itemsGroup.addElement("item_name").setText("gm");
- itemsGroup.addElement("item_desc").setText("gmdesc");
- Element attachment = e.addElement("attachments");
- Element attachmentGroup = attachment.addElement("attachment-group");
- attachmentGroup.addElement("attachment_url").setText("http://china.gm.com/pics/com.cn.gm/story/frsc.jpg");
- attachmentGroup.addElement("attachment_name").setText("黄晓豫");
- Element attachmentDesc = attachmentGroup.addElement("attachment_desc");
- attachmentDesc.addElement("desc1").setText("attach-desc1");
- attachmentDesc.addElement("desc2").setText("attach-desc2");
- attachmentDesc.addElement("desc3").setText("attach-desc3");
- attachmentDesc.addElement("desc4").setText("attach-desc4");
- attachmentGroup = attachment.addElement("attachment-group");
- attachmentGroup.addElement("attachment_url").setText("http://china.gm.com/pics/com.cn.gm/story/wr.jpg");
- attachmentGroup.addElement("attachment_name").setText("张中汉");
- attachmentDesc = attachmentGroup.addElement("attachment_desc");
- attachmentDesc.addElement("desc1").setText("attach-desc1");
- attachmentDesc.addElement("desc2").setText("attach-desc2");
- attachmentDesc = attachmentGroup.addElement("attachment_desc");
- attachmentDesc.addElement("desc1").setText("attach-desc1");
- attachmentDesc.addElement("desc2").setText("attach-desc2");
- Element descGroup = e.addElement("desc_group");
- descGroup.addElement("desc_item").setText("desc_item_1");
- descGroup.addElement("desc_item").setText("desc_item_2");
- descGroup.addElement("desc_item").setText("desc_item_3");
- descGroup.addElement("desc_item").setText("desc_item_4");
- descGroup.addElement("desc_item").setText("desc_item_5");
- return doc;
- }
- }
下面是main调用类:
- package xml.dom4j;
- import java.util.Map;
- import org.dom4j.Document;
- public class Demo {
- public static void main(String[] args){
- CreateXml create = new CreateXml();
- Document doc = create.createXml();
- String strXml = doc.asXML();
- System.out.println(strXml);
- Map map = XmlUtils.Dom2Map(doc);
- System.out.println(map.toString());
- }
- }