日期:2014-05-16 浏览次数:20511 次
?
转自:http://blog.csdn.net/arjick/article/details/6251777
?
需要json-lib-2.1-jdk15.jar和xom-1.2.5.jar,maven pom.xml如下:
?
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.1</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>xom</groupId> <artifactId>xom</artifactId> <version>1.2.5</version> <type>jar</type> <scope>compile</scope> </dependency>?
?
package com.shine.framework.core.util;
import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import net.sf.json.JSON; import net.sf.json.JSONSerializer; import net.sf.json.xml.XMLSerializer; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; /** * common xml conver utility * * @author viruscodecn@gmail.com * @version Framework 2010.10.26 * @url http://blog.csdn.net/arjick/article/details/6251777 */ public class XmlConverUtil { /** * map to xml xml <node><key label="key1">value1</key><key * label="key2">value2</key>......</node> * * @param map * @return */ public static String maptoXml(Map map) { Document document = DocumentHelper.createDocument(); Element nodeElement = document.addElement("node"); for (Object obj : map.keySet()) { Element keyElement = nodeElement.addElement("key"); keyElement.addAttribute("label", String.valueOf(obj)); keyElement.setText(String.valueOf(map.get(obj))); } return doc2String(document); } /** * list to xml xml <nodes><node><key label="key1">value1</key><key * label="key2">value2</key>......</node><node><key * label="key1">value1</key><key * label="key2">value2</key>......</node></nodes> * * @param list * @return */ public static String listtoXml(List list) throws Exception { Document document = DocumentHelper.createDocument(); Element nodesElement = document.addElement("nodes"); int i = 0; for (Object o : list) { Element nodeElement = nodesElement.addElement("node"); if (o instanceof Map) { for (Object obj : ((Map) o).keySet()) { Element keyElement = nodeElement.addElement("key"); keyElement.addAttribute("label", String.valueOf(obj)); keyElement.setText(String.valueOf(((Map) o).get(obj))); } } else { Element keyElement = nodeElement.addElement("key"); keyElement.addAttribute("label", String.valueOf(i)); keyElement.setText(String.valueOf(o)); } i++; } return doc2String(document); } /** * json to xml {"node":{"key":{"@label":"key1","#text":"value1"}}} conver * <o><node class="object"><key class="object" * label="key1">value1</key></node></o> * * @param json * @return */ public static String jsontoXml(String json) { try { XMLSerializer serializer = new XMLSerializer(); JSON jsonObject = JSONSerializer.toJSON(json); return serializer.write(jsonObject); } catch (Exception e) { e.printStackTrace(); } return null; } /** * xml to map xml <node><key label="key1">value1</key><key * label="key2">value2</key>......</node> * * @param xml * @return */ public static Map xmltoMap(String xml) { try { Map map = new HashMap(); Document document = DocumentHelper.parseText(xml); Element nodeElement = document.getRootElement(); List node = nodeElement.elements(); for (Iterator it = node.iterator(); i