java中xml转json
之前做了json转xml,想着继续做一个xml转json吧。 百度了一下,方法很是简单,不过对于节点中的属性没有很好的区分,具体的解决方案改天再研究。因为这个真的简单,只用了两个jar包 org.json和commons-io。jar包下载直接去maven仓库搜就行了,地址 https://mvnrepository.com/artifact/org.jeewxframework/codegenerate/3.4.7
下面上代码
public class XmlToJson {
public static String xml2jsonString() throws JSONException, IOException {
InputStream in = XmlToJson.class.getResourceAsStream("test.xml");
String xml = IOUtils.toString(in);
JSONObject xmlJSONObj = XML.toJSONObject(xml);
return xmlJSONObj.toString();
}
public static void main(String[] args) throws JSONException, IOException {
String string = xml2jsonString();
System.out.println(string);
}
}
效果如下图(test.xml和java文件在同路径下,所以直接写名字就行了)
原博主地址:https://blog.****.net/a532672728/article/details/76312475