java xmltojson jsontoxml
Posted js1314
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java xmltojson jsontoxml相关的知识,希望对你有一定的参考价值。
JSONObject.fromObject需要的有额外的6个包,必不可少,一定要注意:
commons-beanutils-1.9.2.jar
commons-collections-3.2.2.jar
commons-lang-2.6.jar
commons-logging-1.2.jar
ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar
package com.ekingwin.bas.xmljson.utils;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;
public class xmljson
public static String xmlToJson(String xmlContent)
if ((xmlContent == null) || ("".equals(xmlContent)))
return "";
xmlContent = xmlContent.replace("\r", "");
xmlContent = xmlContent.replace("\n", "");
xmlContent = xmlContent.replace("\t", "");
xmlContent = xmlContent.replace(" ", "");
xmlContent = xmlContent.replace("<ITEM>", " <ITEM>");
xmlContent = xmlContent.replace("xmlns", " xmlns");
xmlContent = xmlContent.replace("<e xmlns=\"http://byd.eai.sap\" class=\"object\">", " <e xmlns=\"http://byd.eai.sap\" class=\"object\">");
String jsonContent = "";
XMLSerializer xmlSerializernew = new XMLSerializer();
xmlSerializernew.setSkipNamespaces(true);
xmlSerializernew.setRemoveNamespacePrefixFromElements(true);
jsonContent = xmlSerializernew.read(xmlContent.toString()).toString();
jsonContent = jsonContent.replace("null", "\"\"").replace("[]", "\"\"");
return jsonContent;
public static String jsonToXml(String jsonContent)
String xmlContent = "";
XMLSerializer xmlSerializernew = new XMLSerializer();
xmlSerializernew.setTrimSpaces(true);
if ((jsonContent.startsWith("[")) && (jsonContent.endsWith("]")))
jsonContent =jsonContent.replaceAll(":\\s*(\\d+\\.\\d+)",":\"$1\"");
xmlContent = xmlSerializernew.write(JSONArray.fromObject(jsonContent));
xmlContent = xmlContent.replace("<e class=\"object\">", "<ITEM>");
xmlContent = xmlContent.replace("<e type=\"number\">", "<ITEM>");
xmlContent = xmlContent.replace("</e>", "</ITEM>");
xmlContent = xmlContent.replace("<o>", "");
xmlContent = xmlContent.replace("</o>", "");
xmlContent = xmlContent.replace(" class=\"object\"", "");
xmlContent = xmlContent.replace(" type=\"string\"", "");
xmlContent = xmlContent.replace(" class=\"array\"", "");
xmlContent = xmlContent.replace(" null=\"true\"", "");
xmlContent = xmlContent.replace(" type=\"number\"", "");
else
jsonContent =jsonContent.replaceAll(":\\s*(\\d+\\.\\d+)",":\"$1\"");
xmlContent = xmlSerializernew.write(JSONSerializer.toJSON(jsonContent));
xmlContent = xmlContent.replace("<e class=\"object\">", "<ITEM>");
xmlContent = xmlContent.replace("<e type=\"number\">", "<ITEM>");
xmlContent = xmlContent.replace("</e>", "</ITEM>");
xmlContent = xmlContent.replace("<o>", "");
xmlContent = xmlContent.replace("</o>", "");
xmlContent = xmlContent.replace(" class=\"object\"", "");
xmlContent = xmlContent.replace(" type=\"string\"", "");
xmlContent = xmlContent.replace(" class=\"array\"", "");
xmlContent = xmlContent.replace(" null=\"true\"", "");
xmlContent = xmlContent.replace(" type=\"number\"", "");
return xmlContent;
public static void main(String[] args)
String a = "\"Request\": \r\n" +
" \"head\": \r\n" +
" \"BIZTRANSACTIONID\": \"qwertyuioplkjhgfd\",\r\n" +
" \"COUNT\": \"1\",\r\n" +
" \"CONSUMER\": \"ERP\",\r\n" +
" \"SRVLEVEL\": \"1\",\r\n" +
" \"ACCOUNT\": \"\",\r\n" +
" \"PASSWORD\": \"\"\r\n" +
",\r\n" +
"\r\n" +
" \"List\": [\r\n" +
" \"id\": 1.78978979,\r\n" +
" \"name\": 3546545646.23165778789,\r\n" +
" \"phone\": 12345678,\r\n" +
" \"address\": 66554.3689745,\r\n" +
" \"mail\": \"321@test.com\"\r\n" +
" \r\n" +
" ]\r\n" +
"\r\n" +
"";
String b = "<Request><List><ITEM><address>66554.3689745</address><id>1.78978979</id><mail>321@test.com</mail><name>3546545646.23165778789</name><phone>12345678</phone></ITEM></List><head><ACCOUNT/><BIZTRANSACTIONID>qwertyuioplkjhgfd</BIZTRANSACTIONID><CONSUMER>ERP</CONSUMER><COUNT>1</COUNT><PASSWORD/><SRVLEVEL>1</SRVLEVEL></head></Request>\r\n" +
"";
System.out.println(jsonToXml(a));
System.out.println(xmlToJson(a));
以上是关于java xmltojson jsontoxml的主要内容,如果未能解决你的问题,请参考以下文章