简体   繁体   中英

How to convert JSON to a specified XML structure in JAVA

I am new to JAVA and I faced the following problem.

I have a JSON file and the target is to convert to XML. Conversion should be in the following format.

<a attribute1 = "" attribute2 = "" />

but the conversion happened as follows

<a><attribute1>value</attribute1><attribute2>value<attribute2></a>

How exactly can i convert maintaining the desired XML format?

##Use json.jar for creating JSON to xml 
Steps:
1 .Add below mentioned artifacts to pom


<dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20200518</version>
 </dependency>

2.Use below code template for converting JOSN to xml       
        JSONObject obj = new JSONObject(json_data);
        String xml_data = XML.toString(obj);

No standard library for JSON-to-XML or XML-to-JSON is ever going to give you exactly the format you want. Let the library create the XML it creates, and then transform it using an XSLT stylesheet. Alternatively do a custom conversion all the way from JSON to XML using XSLT 3.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM