public static void main(String[] args) throws Exception { InputStream is = ConvertJSONtoXMLNoHints.class.getResourceAsStream("sample-json.txt"); String jsonData = IOUtils.toString(is); XMLSerializer serializer = new XMLSerializer(); JSON json = JSONSerializer.toJSON(jsonData); serializer.setTypeHintsEnabled(false); String xml = serializer.write(json); System.out.println(xml); }
/** * 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; }
@Test public void testJSONObjectToXMLTest002() { JSONObject jsonObject = JSONObject.fromObject(new Contact()); try { XMLSerializer xml = new XMLSerializer(); String xmlStr = xml.write(jsonObject); System.out.println("testJSONObjectToXMLTest002:" + xmlStr); } catch (Exception e) { e.printStackTrace(); } }
public static String jsontoXml(byte[] josonSource) { String source = ""; try { source = new String(josonSource, "gb2312"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } XMLSerializer xmlSerializer = new XMLSerializer(); JSON json = JSONObject.fromObject(source); ; return xmlSerializer.write(json).toString(); }
public static String jsontoXml(String jsonString) { XMLSerializer xmlSerializer = new XMLSerializer(); JSONObject jsonobj = JSONObject.fromObject(jsonString); return xmlSerializer.write(jsonobj).toString(); }