把xml转换成map

前端之家收集整理的这篇文章主要介绍了把xml转换成map前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
传过来一个xml文件,需要转换成Map,能够应对不用结构的xml,而不是只针对固定格式的xml.
转换规则:
1.主要是Map与List的互相嵌套
2.同名称的节点会被装进List

如下面的xml文件:
Xml代码
  1. @H_404_20@<?xmlversion="1.0"encoding="UTF-8"?>
  2. <GmMail>
  3. template_id>2</to_address>test@corp.gm.comto_alias>tommessage>XML-未来世界Messagerecipient_name>xml-tomsend_address>xml-test@163.comsender_comp_name>xml-gmsubject>xml-helloguysselected_itemsitem_groupitem_url>http://china.gm.comitem_name>gm中文item_desc>gm中文站descitem_desc2>gm中文站desc2item_desc3>gm中文站desc3item_desc4>gm中文站desc4items_source>products>http://www.gm.com>gm>gmdescattachmentsattachment-groupattachment_url @H_404_20@http://china.gm.com/pics/com.cn.gm/story/frsc.jpg
  4. attachment_name>黄晓豫attachment_descdesc1>attach-desc1desc2>attach-desc2desc3>attach-desc3desc4>attach-desc4 @H_404_20@http://china.gm.com/pics/com.cn.gm/story/wr.jpg
  5. >张中汉desc_groupdesc_item>desc_item_1>desc_item_2>desc_item_3>desc_item_4>desc_item_5>

转换后的的Map(转换成字符串的效果):
Java代码
    @H_404_20@{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-helloguys,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:
@H_404_20@packagexml.dom4j;
  • @H_404_20@
  • importjava.util.ArrayList;
  • importjava.util.HashMap;
  • importjava.util.Iterator;
  • importjava.util.List;
  • importjava.util.Map;
  • importorg.dom4j.Document;
  • importorg.dom4j.Element;
  • publicclassXmlUtils{
  • staticMap<String,Object>Dom2Map(Documentdoc){
  • @H_404_20@Map<String,Object>map=newHashMap<String,Object>();
  • if(doc==null)
  • returnmap;
  • @H_404_20@Elementroot=doc.getRootElement();
  • for(Iteratoriterator=root.elementIterator();iterator.hasNext();){
  • @H_404_20@Elemente=(Element)iterator.next();
  • @H_404_20@//System.out.println(e.getName());
  • @H_404_20@Listlist=e.elements();
  • if(list.size()>0){
  • @H_404_20@map.put(e.getName(),Dom2Map(e));
  • @H_404_20@}else
  • @H_404_20@}
  • staticMapDom2Map(Elemente){
  • @H_404_20@Mapmap=newHashMap();
  • for(inti=0;i<list.size();i++){
  • @H_404_20@Elementiter=(Element)list.get(i);
  • @H_404_20@ListmapList=newArrayList();
  • if(iter.elements().size()> @H_404_20@Mapm=Dom2Map(iter);
  • if(map.get(iter.getName())!=null){
  • @H_404_20@Objectobj=map.get(iter.getName());
  • if(!obj.getClass().getName().equals("java.util.ArrayList")){
  • @H_404_20@mapList= @H_404_20@mapList.add(obj);
  • @H_404_20@mapList.add(m);
  • if(obj.getClass().getName().equals("java.util.ArrayList")){
  • @H_404_20@mapList=(List)obj;
  • @H_404_20@map.put(iter.getName(),mapList);
  • else{
  • @H_404_20@mapList.add(iter.getText());
  • @H_404_20@}

  • 下面是一个创建xml的类(不重要,可以忽略,只是用来生成一个xml以便测试,可以自己写)
    importorg.dom4j.DocumentHelper;
  • classCreateXml{
  • /**
  • *@paramargs
  • */
  • staticvoidmain(String[]args){
  • //TODOAuto-generatedmethodstub
  • publicDocumentcreateXml(){
  • @H_404_20@Documentdoc=DocumentHelper.createDocument();
  • @H_404_20@Elemente=doc.addElement("GmMail");
  • @H_404_20@e.addElement("template_id").setText("2");
  • @H_404_20@e.addElement("to_address").setText("test@corp.gm.com");
  • @H_404_20@e.addElement("to_alias").setText("tom");
  • @H_404_20@e.addElement("message").setText("XML-未来世界Message");
  • @H_404_20@e.addElement("recipient_name").setText("xml-tom");
  • @H_404_20@e.addElement("send_address").setText("xml-test@163.com");
  • @H_404_20@e.addElement("sender_comp_name").setText("xml-gm");
  • @H_404_20@e.addElement("subject").setText("xml-helloguys");
  • @H_404_20@ElementSelecteditems=e.addElement("selected_items");
  • @H_404_20@ElementitemsGroup=Selecteditems.addElement("item_group");
  • //Elementitem=itemsGroup.addElement("item");
  • @H_404_20@itemsGroup.addElement("item_url").setText("http://china.gm.com");
  • @H_404_20@itemsGroup.addElement("item_name").setText("gm中文站");
  • @H_404_20@itemsGroup.addElement("item_desc").setText("gm中文站desc");
  • @H_404_20@itemsGroup.addElement("item_desc2").setText("gm中文站desc2");
  • @H_404_20@itemsGroup.addElement("item_desc3").setText("gm中文站desc3");
  • @H_404_20@itemsGroup.addElement("item_desc4").setText("gm中文站desc4");
  • @H_404_20@Selecteditems.addElement("items_source").setText("products");
  • @H_404_20@itemsGroup=Selecteditems.addElement("item_group");
  • @H_404_20@itemsGroup.addElement("item_url").setText("http://www.gm.com");
  • @H_404_20@itemsGroup.addElement("item_name").setText("gm");
  • @H_404_20@itemsGroup.addElement("item_desc").setText("gmdesc");
  • @H_404_20@Elementattachment=e.addElement("attachments");
  • @H_404_20@ElementattachmentGroup=attachment.addElement("attachment-group");
  • @H_404_20@attachmentGroup.addElement("attachment_url").setText("http://china.gm.com/pics/com.cn.gm/story/frsc.jpg");
  • @H_404_20@attachmentGroup.addElement("attachment_name").setText("黄晓豫");
  • @H_404_20@ElementattachmentDesc=attachmentGroup.addElement("attachment_desc");
  • @H_404_20@attachmentDesc.addElement("desc1").setText("attach-desc1");
  • @H_404_20@attachmentDesc.addElement("desc2").setText("attach-desc2");
  • @H_404_20@attachmentDesc.addElement("desc3").setText("attach-desc3");
  • @H_404_20@attachmentDesc.addElement("desc4").setText("attach-desc4");
  • @H_404_20@attachmentGroup=attachment.addElement("attachment-group");
  • @H_404_20@attachmentGroup.addElement("attachment_url").setText("http://china.gm.com/pics/com.cn.gm/story/wr.jpg");
  • @H_404_20@attachmentGroup.addElement("attachment_name").setText("张中汉");
  • @H_404_20@attachmentDesc=attachmentGroup.addElement("attachment_desc");
  • @H_404_20@ElementdescGroup=e.addElement("desc_group");
  • @H_404_20@descGroup.addElement("desc_item").setText("desc_item_1");
  • @H_404_20@descGroup.addElement("desc_item").setText("desc_item_2");
  • @H_404_20@descGroup.addElement("desc_item").setText("desc_item_3");
  • @H_404_20@descGroup.addElement("desc_item").setText("desc_item_4");
  • @H_404_20@descGroup.addElement("desc_item").setText("desc_item_5");
  • returndoc;
  • 下面是main调用类:
    Java代码
      classDemo{
    1. @H_404_20@CreateXmlcreate=newCreateXml();
    2. @H_404_20@Documentdoc=create.createXml();
    3. @H_404_20@StringstrXml=doc.asXML();
    4. @H_404_20@System.out.println(strXml);
    5. @H_404_20@Mapmap=XmlUtils.Dom2Map(doc);
    6. @H_404_20@System.out.println(map.toString());
    7. @H_404_20@}
  • 猜你在找的XML相关文章