把xml转换成map

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

如下面的xml文件:
Xml代码
  1. <?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 http://china.gm.com/pics/com.cn.gm/story/frsc.jpg
  4. attachment_name>黄晓豫attachment_descdesc1>attach-desc1desc2>attach-desc2desc3>attach-desc3desc4>attach-desc4 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代码
    {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_301_198@packagexml.dom4j;
  • importjava.util.ArrayList;
  • importjava.util.HashMap;
  • importjava.util.Iterator;
  • importjava.util.List;
  • importjava.util.Map;
  • importorg.dom4j.Document;
  • importorg.dom4j.Element;
  • public@H_301_198@classXmlUtils{
  • staticMap<String,Object>Dom2Map(Documentdoc){
  • Map<String,Object>map=@H_301_198@newHashMap<String,Object>();
  • if(doc==@H_301_198@null)
  • returnmap;
  • Elementroot=doc.getRootElement();
  • for(Iteratoriterator=root.elementIterator();iterator.hasNext();){
  • Elemente=(Element)iterator.next();
  • //System.out.println(e.getName());
  • Listlist=e.elements();
  • if(list.size()>0){
  • map.put(e.getName(),Dom2Map(e));
  • }@H_301_198@else
  • }
  • staticMapDom2Map(Elemente){
  • Mapmap=@H_301_198@newHashMap();
  • for(@H_301_198@inti=0;i<list.size();i++){
  • Elementiter=(Element)list.get(i);
  • ListmapList=@H_301_198@newArrayList();
  • if(iter.elements().size()> Mapm=Dom2Map(iter);
  • if(map.get(iter.getName())!=@H_301_198@null){
  • Objectobj=map.get(iter.getName());
  • if(!obj.getClass().getName().equals("java.util.ArrayList")){
  • mapList= mapList.add(obj);
  • mapList.add(m);
  • if(obj.getClass().getName().equals("java.util.ArrayList")){
  • mapList=(List)obj;
  • map.put(iter.getName(),mapList);
  • else{
  • mapList.add(iter.getText());
  • }

  • 下面是一个创建xml的类(不重要,可以忽略,只是用来生成一个xml以便测试,可以自己写)
    importorg.dom4j.DocumentHelper;
  • classCreateXml{
  • /**
  • *@paramargs
  • */
  • static@H_301_198@voidmain(String[]args){
  • //TODOAuto-generatedmethodstub
  • publicDocumentcreateXml(){
  • Documentdoc=DocumentHelper.createDocument();
  • Elemente=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-helloguys");
  • ElementSelecteditems=e.addElement("selected_items");
  • ElementitemsGroup=Selecteditems.addElement("item_group");
  • //Elementitem=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");
  • Elementattachment=e.addElement("attachments");
  • ElementattachmentGroup=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("黄晓豫");
  • ElementattachmentDesc=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");
  • ElementdescGroup=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");
  • returndoc;
  • 下面是main调用类:
    Java代码
      classDemo{
    1. CreateXmlcreate=@H_301_198@newCreateXml();
    2. Documentdoc=create.createXml();
    3. StringstrXml=doc.asXML();
    4. System.out.println(strXml);
    5. Mapmap=XmlUtils.Dom2Map(doc);
    6. System.out.println(map.toString());
    7. }
  • 原文链接:https://www.f2er.com/xml/299534.html

    猜你在找的XML相关文章