xml字符串转json字符串
XML字符串转JSON字符串网上的方法很多,这里主要推荐两种:
1、使用org.json包
jar地址:http://mvnrepository.com/artifact/org.json/json
示例代码:
import org.json.JSONException; import org.json.JSONObject; import org.json.XML; public class JsonUtils { public static String xml2jsonString(String xml)throws JSONException{ JSONObject xmlJSONObj = XML.toJSONObject(xml); return xmlJSONObj.toString(); } }
2、使用org.jdom包
jar地址:http://www.jdom.org/
import java.io.ByteArrayInputStream; import java.io.File; import java.io.InputStream; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import net.sf.json.JSONObject; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; public class Xml2JsonUtil { /** * 转换一个xml格式的字符串到json格式 * * @param xml * xml格式的字符串 * @return 成功返回json 格式的字符串;失败反回null */ @SuppressWarnings("unchecked") public static String xml2JSON(String xml) { JSONObject obj = new JSONObject(); try { InputStream is = new ByteArrayInputStream(xml.getBytes("utf-8")); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(is); Element root = doc.getRootElement(); obj.put(root.getName(), iterateElement(root)); return obj.toString(); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 转换一个xml格式的字符串到json格式 * * @param file * java.io.File实例是一个有效的xml文件 * @return 成功反回json 格式的字符串;失败反回null */ @SuppressWarnings("unchecked") public static String xml2JSON(File file) { JSONObject obj = new JSONObject(); try { SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(file); Element root = doc.getRootElement(); obj.put(root.getName(), iterateElement(root)); return obj.toString(); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 一个迭代方法 * * @param element * : org.jdom.Element * @return java.util.Map 实例 */ @SuppressWarnings("unchecked") private static Map iterateElement(Element element) { List jiedian = element.getChildren(); Element et = null; Map obj = new HashMap(); List list = null; for (int i = 0; i < jiedian.size(); i++) { list = new LinkedList(); et = (Element) jiedian.get(i); if (et.getTextTrim().equals("")) { if (et.getChildren().size() == 0) continue; if (obj.containsKey(et.getName())) { list = (List) obj.get(et.getName()); } list.add(iterateElement(et)); obj.put(et.getName(), list); } else { if (obj.containsKey(et.getName())) { list = (List) obj.get(et.getName()); } list.add(et.getTextTrim()); obj.put(et.getName(), list); } } return obj; } // 测试 public static void main(String[] args) { System.out.println( Xml2JsonUtil.xml2JSON("<MapSet>" + "<MapGroup id='Sheboygan'>" + "<Map>" + "<Type>MapGuideddddddd</Type>" + "<SingleTile>true</SingleTile>" + "<Extension>" + "<ResourceId>ddd</ResourceId>" + "</Extension>" + "</Map>" + "<Map>" + "<Type>ccc</Type>" + "<SingleTile>ggg</SingleTile>" + "<Extension>" + "<ResourceId>aaa</ResourceId>" + "</Extension>" + "</Map>" + "<Extension />" + "</MapGroup>" + "<ddd>" + "33333333" + "</ddd>" + "<ddd>" + "444" + "</ddd>" + "</MapSet>")); } }
参考:
1)http://blog.csdn.net/lovesummerforever/article/details/26396465
2)http://blog.csdn.net/duguwuyi/article/details/51500852