XML 、 MAP 与 JSON 格式的转换

使用的  第三方的 jar包 为 dom4j,json 包使用的是 fastjson

 

XML 转 Map

 

	public static Map commonXmlToMap(String xml) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Document doc = DocumentHelper.parseText(xml);
			if (doc == null)
				return map;
			Element root = doc.getRootElement();
			for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
				Element e = (Element) iterator.next();
				List list = e.elements();
				if (list.size() > 0) {
					map.put(e.getName(), Dom2Map(e));
				} else
					map.put(e.getName(), e.getText());
			}
		} catch (Exception e) {
			try {
				throw new Exception();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return map;
	}


public static Map Dom2Map(Element e) throws Exception {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;  
}

Map 转 XML

 

public static  String   toXML(Map  _obj,Object key){ 
	    StringBuffer   xml   =   new   StringBuffer(1024);
	    xml.append( "<").append(key).append(">\n"); 
	    xml.append(toXML(_obj)); 
	    xml.append( "</").append(key).append(">"); 
	    return   xml.toString(); 
	    }
	
	private static String toXML(Map _obj) {
		StringBuffer sb = new StringBuffer();
		if (_obj != null) {
			Set keySet = _obj.keySet();
			for (Iterator it = keySet.iterator(); it.hasNext();) {
				Object key = it.next();
				Object value = _obj.get(key);
				if(value instanceof Map){
					sb.append(toXMLMap((Map)value,key));
				}
				else if(value instanceof Collection){
					sb.append(toXMLCollection((Collection)value,key));
				}
				else{
					sb.append(toXMLOtherObject(PMSUtil.isNull(value),key));
				}
			}
		}
		return sb.toString();
	}

	private  static String toXMLCollection(Collection _list,Object key) {
		StringBuffer sb = new StringBuffer();
		if (_list != null) {
			sb.append("<").append(key).append("LIST>").append("\n");
			for (Iterator it = _list.iterator(); it.hasNext();) {
				sb.append("<").append(key).append(">").append("\n");
				sb.append(toXML((Map)it.next()));
				sb.append("</").append(key).append(">").append("\n");
			}
			sb.append("</").append(key).append("LIST>").append("\n ");
		}
		return sb.toString();
	}

	private static String toXMLMap(Map _map,Object node) {
		StringBuffer sb = new StringBuffer();
		if (_map != null) {
			sb.append("<").append(node).append(">").append("\n");
			sb.append(toXML(_map));
			sb.append("</").append(node).append(">").append("\n");
		}
		return sb.toString();
	}

	private static String toXMLOtherObject(Object _obj, Object key) {
		StringBuffer sb = new StringBuffer();
		if (_obj != null) {
			sb.append("<").append(key).append(">");
			sb.append(_obj.toString());
			sb.append("</").append(key).append("> ").append("\n ");
		}
		return sb.toString();
	}

public static void main(String[] args) {
		
		MapToXmlString ma=new MapToXmlString();
		Map map = new HashMap();
		map.put("code", "0");
		map.put("msg", "success");
		System.out.print(ma.toXML(map,"rest"));
	}

  

 

 

 

Map 转 Json

String str = "{\"code\":\"0\",\"msg\":\"success\"}";

 public static Map jasonToMap(String jason) {
        return JSON.parseObject(jason, Map.class);
    }

 

 修改 Json 的值

Map<String,String> value = JSON.parseObject(json,Map.class);
value.put("zzmm","newValue");//改变zzmm的值
JSON.toJSONString(value);//重新转成json字符串,{“name”:"张三","age":"20","xb":“男”,"zzmm":"newValue"}

 

 

 

 

 

你可能感兴趣的:(xml,json)