Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting JSON to XML in Java

Tags:

java

json

xml

xpath

I am new to json. I am having a program to generate xml from json object.

String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}";       JSON json = JSONSerializer.toJSON( str );       XMLSerializer xmlSerializer = new XMLSerializer();       xmlSerializer.setTypeHintsCompatibility( false );       String xml = xmlSerializer.write( json );       System.out.println(xml);  

the output is:

<?xml version="1.0" encoding="UTF-8"?> <o><array json_class="array"><e json_type="number">1</e><e json_type="number">2</e><e json_type="number">3</e></array><boolean json_type="boolean">true</boolean><double json_type="number">2.0</double><integer json_type="number">1</integer><name json_type="string">JSON</name><nested json_class="object"><id json_type="number">42</id></nested></o> 

my biggest problem is how to write my own attributes instead of json_type="number" and also writing my own sub elements like .

like image 762
vinod Avatar asked Nov 14 '13 12:11

vinod


People also ask

Can we convert XML to JSON in Java?

We can convert XML to JSON array using org. json. XML class, this provides a static method, XML. toJSONObject() to convert XML to JSON array.

How do I convert JSON to XML using GSON?

JSON to XML in JAVA is converted by using JSONObject json = new JSONObject(str); String xml = XML. If you have a valid dtd file or the xml file then it is very easy to transform json to xml and also xml to json.

Which utility used by REST services convert JSON format to XML?

The serializeMapToXML utility. REST services convert JSON format to XML format using the com.


2 Answers

Use the (excellent) JSON-Java library from json.org then

JSONObject json = new JSONObject(str); String xml = XML.toString(json); 

toString can take a second argument to provide the name of the XML root node.

This library is also able to convert XML to JSON using XML.toJSONObject(java.lang.String string)

Check the Javadoc

Link to the the github repository

POM

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

original post updated with new links

like image 93
Bruno Grieder Avatar answered Oct 05 '22 23:10

Bruno Grieder


Underscore-java library has static method U.jsonToXml(jsonstring). Live example

import com.github.underscore.U;  public class MyClass {     public static void main(String args[]) {         String json = "{\"name\":\"JSON\",\"integer\":1,\"double\":2.0,\"boolean\":true,\"nested\":{\"id\":42},\"array\":[1,2,3]}";           System.out.println(json);          String xml = U.jsonToXml(json);           System.out.println(xml);      } } 

Output:

{"name":"JSON","integer":1,"double":2.0,"boolean":true,"nested":{"id":42},"array":[1,2,3]} <?xml version="1.0" encoding="UTF-8"?> <root>   <name>JSON</name>   <integer number="true">1</integer>   <double number="true">2.0</double>   <boolean boolean="true">true</boolean>   <nested>     <id number="true">42</id>   </nested>   <array number="true">1</array>   <array number="true">2</array>   <array number="true">3</array> </root> 
like image 27
Valentyn Kolesnikov Avatar answered Oct 06 '22 00:10

Valentyn Kolesnikov