An example of the conversion between XML and JSON in Java


The example of this article describes how to implement the conversion between XML and JSON in Java. To share with you for your reference, as follows:


Recently, the conversion between XML and JSON has been confused. Here’s a demo for future use.


The project format is:

Jar packages are checked out one by one, and they are clean.



package exercise.xml;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;
import org.jdom.Document;
public class XmlExercise {
   *Convert XML string < strong > to JSON string
   * @param xmlString
   *XML string
   *@ return JSON < strong > object < / strong >
  public static String xml2json(String xmlString) {
    XMLSerializer xmlSerializer = new XMLSerializer();
    JSON json =;
    return json.toString(1);
   *Convert XmlDocument < strong > to JSON < strong > object < / strong >
   * @param xmlDocument
   *      XML Document
   *@ return JSON < strong > object < / strong >
  public static String xml2json(Document xmlDocument) {
    return xml2json(xmlDocument.toString());
   *JSON (array) string < strong > Convert < / strong > to XML string
   * @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)); // only JSON arrays are supported in this way

Test class:

package exercise.xml;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class XmlTest extends XmlExercise {
  public static void main(String[] args) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("username", "horizon");
    JSONArray jsonArray = new JSONArray();
    JSONObject dataJson = new JSONObject();
    dataJson.put("data", jsonArray);
    String xml = json2xml(dataJson.toString());
    System.out.println("xml:" + xml);
    String str = xml2json(xml);
    System.out.println("to_json" + str);

PS: here are several related online tools for your reference:

On-lineJSONCode inspection, inspection, beautification and formatting tools:

On-lineXML/JSON mutual conversion tool:

Online formattingXML/Online compressionXML

XMLOnline compression / formatting tool:

XMLCode online formatting beautification tool:

For more information about Java algorithm, please refer to the following topics: Java operation JSON format data skill summary, Java data structure and algorithm tutorial, Java operation DOM node skill summary, java file and directory operation skill summary and Java cache operation skill summary

I hope this article will be helpful to you in Java programming.

Recommended Today

PHP realizes UnionPay business H5 payment

UnionPay business H5 payment interface document: document address 1: H5 payment interface address: 1: Alipay payment Test address: Official address: 2: UnionPay payment Test address: Official address: 2: Basic parameters required by the interface The interface uses get parameters. After the interface parameters are directly put into the interface address, the […]