Java xml to json class

陈思聿 发布于 2013/05/11 13:31
阅读 889
收藏 2
import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.util.Iterator; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.apache.wink.json4j.JSONArray; import org.apache.wink.json4j.JSONException; import org.apache.wink.json4j.JSONObject; /** * Depend on json4j library * @author bitjjj * */ public class JsonToXML { private static StringBuilder result = new StringBuilder( ""); private static String[] spacialChars = { "&", "<", ">", "/"", "'" }; private static String[] validChars = { "&", "<", ">", ""","'" }; /** * @param args * @throws IOException * @throws JSONException */ public static void main(String[] args){ String jsonString = "{InterventionPlan:{" + "'name':'jeffco /" & >ol>'," + "'item':[{title:'title1'},{title:'title2'},{title:'title3'}]," + "obj:{prop1:'val//'ue1',prop2:'value2'}}" + "}"; System.out.println(prettyFormat(toXml(jsonString),4)); } /** * For specail char there are bugs using this way to format xml string * Don't suggest to use this method,just for debugging * @param input * @param indent * @return */ public static String prettyFormat(String input, int indent) { try { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory .newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); } } public static String toXml(String jsonString){ try { JSONObject jsonObject = new JSONObject(jsonString); toXml(jsonObject); } catch (Exception e) { throw new RuntimeException(e); } return result.toString(); } private static void toXml(JSONObject json) throws Exception { Iterator keyIter = json.keys(); while (keyIter.hasNext()) { String key = (String) keyIter.next(); Object jsonValue = json.get(key); if (jsonValue instanceof JSONArray) { JSONArray arrayValue = (JSONArray) jsonValue; for (int i = 0; i < arrayValue.length(); i++) { appendFlagBegin(key); Object arrItem = arrayValue.get(i); if (arrItem instanceof JSONObject) { toXml((JSONObject) arrItem); } else if (arrItem instanceof JSONArray) { String arrItemStr = "{" + key + ":" + ((JSONArray) arrItem).toString() + "}"; toXml(new JSONObject(arrItemStr)); } else { appendText(arrItem.toString()); } appendFlagEnd(key); } } else { appendFlagBegin(key); if (jsonValue instanceof JSONObject) { toXml((JSONObject) jsonValue); } else { appendText(jsonValue.toString()); } appendFlagEnd(key); } } } private static String replaceSpecialChar(String s) { for (int i = 0; i < spacialChars.length; i++) { s = s.replaceAll(spacialChars[i], validChars[i]); } return s; } private static void appendText(String s) { result.append(replaceSpecialChar(s)); } private static void appendFlagBegin(String str) { result.append("<" + str + ">"); } private static void appendFlagEnd(String str) { result.append(""); } }
加载中
0
黄正文
黄正文

我X,这代码,给机器看的吧

返回顶部
顶部