xml和json之間的轉換


/**
* xml轉json
* @author yizw
*
*/
public class XmlExercise {

/**
* 將xml字符串<STRONG>轉換</STRONG>為JSON字符串
*
* @param xmlString
* xml字符串
* @return JSON<STRONG>對象</STRONG>
*/
public static String xml2json(String xmlString) {
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read(xmlString);
return json.toString(1);
}

/**
* 將xmlDocument<STRONG>轉換</STRONG>為JSON<STRONG>對象</STRONG>
*
* @param xmlDocument
* XML Document
* @return JSON<STRONG>對象</STRONG>
*/
public static String xml2json(Document xmlDocument) {
return xml2json(xmlDocument.toString());
}

/**
* JSON(數組)字符串<STRONG>轉換</STRONG>成XML字符串
*
* @param jsonString
* @return
*/
public static String json2xml(String jsonString) {
XMLSerializer xmlSerializer = new XMLSerializer();
return xmlSerializer.write(JSONSerializer.toJSON(jsonString));
// return xmlSerializer.write(JSONArray.fromObject(jsonString));//這種方式只支持JSON數組
}


}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM