xml和json互转,这里给一些示例
https://github.com/smart-fun/XmlToJson
xml转json
String xmlString; // some XML String previously created
XmlToJson xmlToJson = new XmlToJson.Builder(xmlString).build();
// convert to a JSONObject
JSONObject jsonObject = xmlToJson.toJson();
// convert to a Json String
String jsonString = xmlToJson.toString();
// convert to a formatted Json String
String formatted = xmlToJson.toFormattedString();
原xml
<?xml version="1.0" encoding="utf-8"?>
<library>
<owner>John Doe</owner>
<book id="007">James Bond</book>
<book id="000">Book for the dummies</book>
</library>
转换后
{
"library":{
"owner": "John Doe",
"book":[
{
"id":"007",
"content":"James Bond"
},
{
"id":"000",
"content":"Book for the dummies"
}
]
}
}
反之json转xml
JSONObject jsonObject; // some JSONObject previously created
JsonToXml jsonToXml = new JsonToXml.Builder(jsonObject).build();
// Converts to a simple XML String
String xmlString = jsonToXml.toString();
// Converts to a formatted XML String
int indentationSize = 3;
String formattedXml = jsonToXml.toFormattedString(indentationSize);
还有一些自定义名称和属性,大家可以看开源文档。