An example of the conversion between XML and JSON in Java

Time:2020-1-18

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:

Narrator:

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

Topic:

The project format is:

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

Code:

Tools:

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 = xmlSerializer.read(xmlString);
    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();
    jsonArray.add(jsonObject);
    //jsonArray.add(jsonObject);
    dataJson.put("data", jsonArray);
    System.out.println(dataJson.toString());
    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:
http://tools.jb51.net/code/json

On-lineXML/JSON mutual conversion tool:
http://tools.jb51.net/code/xmljson

Online formattingXML/Online compressionXML
http://tools.jb51.net/code/xmlformat

XMLOnline compression / formatting tool:
http://tools.jb51.net/code/xml_format_compress

XMLCode online formatting beautification tool:
http://tools.jb51.net/code/xmlcodeformat

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: http://58.247.0.18:29015/v1/netpay/trade/h5-pay Official address: https://api-mop.chinaums.com/ 2: UnionPay payment Test address: http://58.247.0.18:29015/v1/netpay/uac/order Official address: https://api-mop.chinaums.com/ 2: Basic parameters required by the interface The interface uses get parameters. After the interface parameters are directly put into the interface address, the […]