展开全部
首先楼主的json数据有问题。
下面代码亲测32313133353236313431303231363533e4b893e5b19e31333337613835,可用,两个文件放在同级目录:
Test.javapublic class Test {
public static void main(String[] args) {
ConvertXMLtoJSON();
String jStr = "{\"users\": {\"user\": ["
+ "{\"uid\": \"1\", \"node_id\": \"2\",\"children\": [{\"id\": \"43\",\"value\": \"14\"},{\"id\": \"44\",\"value\": \"15\"}]},"
+ "{\"uid\": \"2\",\"node_id\": \"2\",\"children\": [{\"id\": \"45\",\"value\": \"11\"},{\"id\": \"46\",\"value\": \"11\"},{\"id\": \"47\",\"value\": \"16\"}]},"
+ "{\"uid\": \"3\",\"node_id\": \"2\",\"children\": {\"id\": \"48\",\"value\": \"11\"}}]}"
+ "}";
System.out.println(jsontoXml(jStr));
}
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;
}
public static void ConvertXMLtoJSON() {
InputStream is = Test.class.getResourceAsStream("sample.xml");
String xml;
try {
xml = IOUtils.toString(is);
System.out.println(xml);
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read(xml);
System.out.println(json.toString(1));
}catch (IOException e) {
e.printStackTrace();
}
}
}
sample.xml<?xml version="1.0" encoding="utf-8"?>
43
14
44
15
2
1
45
11
46
11
47
16
2
2
48
11
2
3