Saturday 27 April 2013

JSON To XML Conversion



Hi friends,

I would like to share converting JOSN to XML format.

XML: 

  • XML (Extensible Markup Language) is a mark-up language that defines the set of rules for encoding document in Human &Machine readable format.
  • XML mainly emphasizes on Simplicity, Generality and Usability over the Internet. 
  • XML is a textual data format with strong support for all the languages of the World.
JSON:
  • JSON (JavaScript Object Notation) is a text-based open standard designed for Human-Readable data interchange.
  • Even JSON is javaScript specific, it is language-independent with parsers available for many languages.
  • JSON format is often used over network connection for serializing and transmitting structured data .

On keeping Pros and Cons of them a-part, this is the scenario which is very often needed for developing Applications (Mainly in Web environment).


Pre-Requisites:
I used net.sf.json-lib for JSON support. You can download from here.
Json-lib should contain the below dependencies in classpath:
  • commons-lang
  • commons-beanutils
  • commons-collections
  • commons-logging
  • ezmorph

Below is code for Converting JSON to XML format:

package testapp;
import java.io.InputStream;
import java.net.URL;
import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;
import org.apache.commons.io.IOUtils;
/**
 * @author naveen.k
 */
public class JsonToXMLConverter {
    private URL url = null;
    private InputStream inputStream = null;    
    public void getJsonfromXml() {
        try{
            url = JsonToXMLConverter.class.getClassLoader().getResource("sample.txt");
            inputStream = url.openStream();
            String jsonData = IOUtils.toString(inputStream);
            XMLSerializer serializer = new XMLSerializer();
            JSON json = JSONSerializer.toJSON(jsonData);
            String xml = serializer.write(json);
            System.out.println(xml);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
     try {
                if (inputStream != null) {
                    inputStream.close();
                }
                url = null;
            } catch (IOException ex) {}
        }
    }
    public static void main(String[] args) {
        new JsonToXMLConverter().getJsonfromXml();
    }
}



Place the sample JSON in the specified path

sample.txt
==========

[
{
"@id":"bk01",
"author":"Gambardella, Matthew",
"title":"XML Developer's Guide",
"genre":"Computer",
"price":"44.95",
"publish_date":"2000-10-01",
"description":"An in-depth look at creating applications \n with XML."
},
{
"@id":"bk02",
"author":"Ralls, Kim",
"title":"Midnight Rain",
"genre":"Fantasy",
"price":"5.95",
"publish_date":"2000-12-16",
"description":"A former architect battles corporate zombies, \n an evil sorceress, and her own childhood to become queen \n of the world."
}
]
 


Output XML:
===========

   
  
  
  Gambardella, Matthew 
  An in-depth look at creating applications with XML. 
  Computer 
  44.95 
  2000-10-01 
  XML Developer's Guide 
  
  
  Ralls, Kim 
  A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world. 
  Fantasy 
  5.95 
  2000-12-16 
  Midnight Rain 
  
  


For XML to JSON conversion please refer this

1 comment:

  1. How to avoid at beginning and class, type attributes and all. I just want to convert plain xml from json ??

    ReplyDelete