1、前提须要jar包:
json-lib-2.4-jdk15.jar 和 xom-1.2.5.jar ,maven 仓库:
net.sf.json-lib
json-lib
2.4
jdk15
xom
xom
1.2.5
2、代码部分:
public static JSON xmlToJson(String xml) {
XMLSerializer xmlSerializer = new XMLSerializer();
if(xml!=null && !”“.equals(xml)){
xml = xml.replaceAll(“
|
”, “”);
JSON json = xmlSerializer.read(xml);
return json;
}else{
return null;
}
}
public static void main(String[] args) {
String xmlStr = "<parent><id>01</id<name>marry</name</parent>";
JSON json = xmlToJson(xmlStr);
System.out.println(json.toString());
}
3、结果:
{“id”:”01”,”name”:”marry”}