Map 和 XML 相互转换(JAVA)

XML:DE转换成Map.
再帮忙提供一个逆转的方法。多谢大家。
属性别丢啊

自己弄出来 了。csdn界面现在换的都不知道该点哪里。真不习惯。

package cn.jsfund.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.collections.map.LinkedMap;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import cn.jsfund.web.Validation;

/**

  • XML 工具类
  • @author zhaoxb
  • @time 2013-07-24
    */
    public class XMLUtil {

    private static final String SEPARATOR = " ";
    /**

    • 把XML字符串转换成MAP 声明:这个类还不完善, 不能自动取得主要报文体中的内容。得自己从map中取出报文体。示例 Map FOAReq =
    • map.get("FOAReq")
    • @param xmlStr
    • @return map
      */
      public static Map xml2Map(String xmlStr) {
      Map map = new LinkedHashMap();
      try {
      Document doc;
      doc = DocumentHelper.parseText(xmlStr);
      Element rootElement = doc.getRootElement();
      xml2Map(rootElement, map);

      return map;
      

      } catch (DocumentException e) {
      e.printStackTrace();
      }

      return null;
      }

    /**

    • 递归方法 获得节点的值,如果某节点下还有子节点,则些key对应的value也是一个map
    • @param element
    • @param map */ private static void xml2Map(Element element, Map map) { Iterator i = element.elementIterator(); String key = element.getName(); Map childMap = new LinkedMap(); Iterator i2 = element.attributeIterator(); if (!i.hasNext()) { String value = element.getTextTrim(); map.put(key, value); } else { // Map childMap = new LinkedHashMap(); while (i.hasNext()) { map.put(key, childMap); Element childElement = (Element) i.next(); xml2Map(childElement, childMap); } } if (i2.hasNext()) { Map attrMap = new LinkedMap(); while (i2.hasNext()) { Attribute attribute = (Attribute) i2.next(); String attrName = attribute.getName(); String attrValue = attribute.getValue(); attrMap.put(attrName, attrValue); } map.put("@" + key, attrMap); } }

    /**

    • map转xml字符串
    • @param map
    • 所要转换的map
    • @param businessName
    • 业务名字.比如 开户是FOARes
    • @return String 拼接的 */ public static String map2Xml(Map map) { StringBuffer sbf = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); map2Xml(sbf, map); return sbf.toString(); }

    /**

    • 递归方法将map里的键和值拼接成xml字符串
    • @param sbf
    • @param map */

    private static void map2Xml(StringBuffer sbf, Map map) {
    Set s = map.entrySet();
    Iterator i = s.iterator();
    StringBuffer childSbf = new StringBuffer();
    while(i.hasNext()){
    Entry e = (Entry) i.next();
    String key = (String) e.getKey();
    if(key.indexOf("@") >= 0){
    continue;
    }
    if(key != null){
    childSbf.append("<").append(key);
    }
    String attr = "@" + key;
    Map attrMap = (Map) map.get(attr);
    if(null != attrMap){
    Set attrSet = attrMap.entrySet();
    Iterator attrIterator = attrSet.iterator();
    while(attrIterator.hasNext()){
    Entry attrEntry = (Entry) attrIterator.next();
    String attrKey = (String) attrEntry.getKey();
    String attrValue = (String) attrEntry.getValue();
    childSbf.append(SEPARATOR).append(attrKey).append("=").append("\"").append(attrValue).append("\"");
    }
    }
    childSbf.append(">");
    Object value = e.getValue();
    if(value instanceof List){
    List list = (List) value;
    for(int j = 0; j Map valueMap = (Map) list.get(j);
    map2Xml(childSbf,valueMap);
    }
    }else if(value instanceof Map){
    Map valueMap = (Map) value;
    map2Xml(childSbf, valueMap);
    }else{
    if(null != value){
    childSbf.append(value);
    }else{
    childSbf.append("");
    }
    }
    if(key != null){
    childSbf.append("").append(key).append(">");
    }
    }
    sbf.append(childSbf);

    }

    public static void main(String[] args) {
    String xmlStr6 = "";
    Map map6 = xml2Map(xmlStr6);
    System.out.println(map6.toString());

    String result = map2Xml(map6);
    System.out.println(result);
    

    }
    }

自己弄出来 了。csdn界面现在换的都不知道该点哪里。真不习惯。