1 package com.qbskj.project.util; 2 3 import java.io.ByteArrayOutputStream; 4 import java.util.ArrayList; 5 import java.util.HashMap; 6 import java.util.Iterator; 7 import java.util.List; 8 import java.util.Map; 9 10 import net.sf.json.JSON; 11 import net.sf.json.JSONSerializer; 12 import net.sf.json.xml.XMLSerializer; 13 14 import org.dom4j.Document; 15 import org.dom4j.DocumentHelper; 16 import org.dom4j.Element; 17 import org.dom4j.io.OutputFormat; 18 import org.dom4j.io.XMLWriter; 19 20 /** 21 * common xml conver utility 22 * 23 */ 24 public class XmlConverUtil { 25 26 public final static String CDATA_PREFIX = "CDATA_"; 27 /** 28 * map to xml xml <node><key label="key1">value1</key><key 29 * label="key2">value2</key>......</node> 30 * 31 * @param map 32 * @return 33 */ 34 public static String maptoXml(Map<String,Object> map) { 35 Document document = DocumentHelper.createDocument(); 36 Element nodeElement = document.addElement("xml"); 37 for (Object obj : map.keySet()) { 38 Element keyElement = nodeElement.addElement(String.valueOf(obj)); 39 String vlaue = String.valueOf(map.get(obj)); 40 if(vlaue.startsWith(CDATA_PREFIX)){ 41 keyElement.addCDATA(vlaue.replaceAll(CDATA_PREFIX, "")); 42 }else{ 43 keyElement.setText(vlaue); 44 } 45 } 46 return nodeElement.asXML(); 47 } 48 49 /** 50 * list to xml xml <nodes><node><key label="key1">value1</key><key 51 * label="key2">value2</key>......</node><node><key 52 * label="key1">value1</key><key 53 * label="key2">value2</key>......</node></nodes> 54 * 55 * @param list 56 * @return 57 */ 58 public static String listtoXml(List list) throws Exception { 59 Document document = DocumentHelper.createDocument(); 60 Element nodesElement = document.addElement("nodes"); 61 int i = 0; 62 for (Object o : list) { 63 Element nodeElement = nodesElement.addElement("node"); 64 if (o instanceof Map) { 65 for (Object obj : ((Map) o).keySet()) { 66 Element keyElement = nodeElement.addElement("key"); 67 keyElement.addAttribute("label", String.valueOf(obj)); 68 keyElement.setText(String.valueOf(((Map) o).get(obj))); 69 } 70 } else { 71 Element keyElement = nodeElement.addElement("key"); 72 keyElement.addAttribute("label", String.valueOf(i)); 73 keyElement.setText(String.valueOf(o)); 74 } 75 i++; 76 } 77 return doc2String(document); 78 } 79 80 /** 81 * json to xml {"node":{"key":{"@label":"key1","#text":"value1"}}} conver 82 * <o><node class="object"><key class="object" 83 * label="key1">value1</key></node></o> 84 * 85 * @param json 86 * @return 87 */ 88 public static String jsontoXml(String json) { 89 try { 90 XMLSerializer serializer = new XMLSerializer(); 91 JSON jsonObject = JSONSerializer.toJSON(json); 92 return serializer.write(jsonObject); 93 } catch (Exception e) { 94 e.printStackTrace(); 95 } 96 return null; 97 } 98 99 /** 100 * xml to map xml <node><key label="key1">value1</key><key 101 * label="key2">value2</key>......</node> 102 * 103 * @param xml 104 * @return 105 */ 106 @SuppressWarnings("unchecked") 107 public static Map<String,Object> xmltoMap(String xml) { 108 try { 109 Map<String,Object> map = new HashMap<String,Object>(); 110 Document document = DocumentHelper.parseText(xml); 111 Element nodeElement = document.getRootElement(); 112 List<Object> node = nodeElement.elements(); 113 for (Iterator<Object> it = node.iterator(); it.hasNext();) { 114 Element elm = (Element) it.next(); 115 map.put(elm.getName(), elm.getText()); 116 elm = null; 117 } 118 node = null; 119 nodeElement = null; 120 document = null; 121 return map; 122 } catch (Exception e) { 123 e.printStackTrace(); 124 } 125 return null; 126 } 127 128 /** 129 * xml to list xml <nodes><node><key label="key1">value1</key><key 130 * label="key2">value2</key>......</node><node><key 131 * label="key1">value1</key><key 132 * label="key2">value2</key>......</node></nodes> 133 * 134 * @param xml 135 * @return 136 */ 137 public static List xmltoList(String xml) { 138 try { 139 List<Map> list = new ArrayList<Map>(); 140 Document document = DocumentHelper.parseText(xml); 141 Element nodesElement = document.getRootElement(); 142 List nodes = nodesElement.elements(); 143 for (Iterator its = nodes.iterator(); its.hasNext();) { 144 Element nodeElement = (Element) its.next(); 145 Map map = xmltoMap(nodeElement.asXML()); 146 list.add(map); 147 map = null; 148 } 149 nodes = null; 150 nodesElement = null; 151 document = null; 152 return list; 153 } catch (Exception e) { 154 e.printStackTrace(); 155 } 156 return null; 157 } 158 159 /** 160 * xml to json <node><key label="key1">value1</key></node> 转化为 161 * {"key":{"@label":"key1","#text":"value1"}} 162 * 163 * @param xml 164 * @return 165 */ 166 public static String xmltoJson(String xml) { 167 XMLSerializer xmlSerializer = new XMLSerializer(); 168 return xmlSerializer.read(xml).toString(); 169 } 170 171 /** 172 * 173 * @param document 174 * @return 175 */ 176 public static String doc2String(Document document) { 177 String s = ""; 178 try { 179 // 使用输出流来进行转化 180 ByteArrayOutputStream out = new ByteArrayOutputStream(); 181 // 使用UTF-8编码 182 OutputFormat format = new OutputFormat(" ", true, "UTF-8"); 183 XMLWriter writer = new XMLWriter(out, format); 184 writer.write(document); 185 s = out.toString("UTF-8"); 186 } catch (Exception ex) { 187 ex.printStackTrace(); 188 } 189 return s; 190 } 191 }