converting XML to JSON object

Hi all here is my sudden requirement of how to get xml to json object in my project so just gone and researched for a while and came up with the below code which can generate a formated Json object. following code is simple to understand. this project is configured with Maven.

below is the xml that i want to convert to JSON object.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<CATALOG>
	<CD>
		<TITLE>Empire Burlesque</TITLE>
		<ARTIST>Bob Dylan</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Columbia</COMPANY>
		<PRICE>10.90</PRICE>
		<YEAR>1985</YEAR>
	</CD>
	<CD>
		<TITLE>Hide your heart</TITLE>
		<ARTIST>Bonnie Tyler</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>CBS Records</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1988</YEAR>
	</CD>
	<CD>
		<TITLE>Greatest Hits</TITLE>
		<ARTIST>Dolly Parton</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>RCA</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1982</YEAR>
	</CD>
	<CD>
		<TITLE>Still got the blues</TITLE>
		<ARTIST>Gary Moore</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Virgin records</COMPANY>
		<PRICE>10.20</PRICE>
		<YEAR>1990</YEAR>
	</CD>
	<CD>
		<TITLE>Eros</TITLE>
		<ARTIST>Eros Ramazzotti</ARTIST>
		<COUNTRY>EU</COUNTRY>
		<COMPANY>BMG</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1997</YEAR>
	</CD>
	<CD>
		<TITLE>One night only</TITLE>
		<ARTIST>Bee Gees</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Polydor</COMPANY>
		<PRICE>10.90</PRICE>
		<YEAR>1998</YEAR>
	</CD>
	<CD>
		<TITLE>Sylvias Mother</TITLE>
		<ARTIST>Dr.Hook</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>CBS</COMPANY>
		<PRICE>8.10</PRICE>
		<YEAR>1973</YEAR>
	</CD>
	<CD>
		<TITLE>Maggie May</TITLE>
		<ARTIST>Rod Stewart</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Pickwick</COMPANY>
		<PRICE>8.50</PRICE>
		<YEAR>1990</YEAR>
	</CD>
	<CD>
		<TITLE>Romanza</TITLE>
		<ARTIST>Andrea Bocelli</ARTIST>
		<COUNTRY>EU</COUNTRY>
		<COMPANY>Polydor</COMPANY>
		<PRICE>10.80</PRICE>
		<YEAR>1996</YEAR>
	</CD>
	<CD>
		<TITLE>When a man loves a woman</TITLE>
		<ARTIST>Percy Sledge</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Atlantic</COMPANY>
		<PRICE>8.70</PRICE>
		<YEAR>1987</YEAR>
	</CD>
	<CD>
		<TITLE>Black angel</TITLE>
		<ARTIST>Savage Rose</ARTIST>
		<COUNTRY>EU</COUNTRY>
		<COMPANY>Mega</COMPANY>
		<PRICE>10.90</PRICE>
		<YEAR>1995</YEAR>
	</CD>
	<CD>
		<TITLE>1999 Grammy Nominees</TITLE>
		<ARTIST>Many</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Grammy</COMPANY>
		<PRICE>10.20</PRICE>
		<YEAR>1999</YEAR>
	</CD>
	<CD>
		<TITLE>For the good times</TITLE>
		<ARTIST>Kenny Rogers</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Mucik Master</COMPANY>
		<PRICE>8.70</PRICE>
		<YEAR>1995</YEAR>
	</CD>
	<CD>
		<TITLE>Big Willie style</TITLE>
		<ARTIST>Will Smith</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Columbia</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1997</YEAR>
	</CD>
	<CD>
		<TITLE>Tupelo Honey</TITLE>
		<ARTIST>Van Morrison</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Polydor</COMPANY>
		<PRICE>8.20</PRICE>
		<YEAR>1971</YEAR>
	</CD>
	<CD>
		<TITLE>Soulsville</TITLE>
		<ARTIST>Jorn Hoel</ARTIST>
		<COUNTRY>Norway</COUNTRY>
		<COMPANY>WEA</COMPANY>
		<PRICE>7.90</PRICE>
		<YEAR>1996</YEAR>
	</CD>
	<CD>
		<TITLE>The very best of</TITLE>
		<ARTIST>Cat Stevens</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Island</COMPANY>
		<PRICE>8.90</PRICE>
		<YEAR>1990</YEAR>
	</CD>
	<CD>
		<TITLE>Stop</TITLE>
		<ARTIST>Sam Brown</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>A and M</COMPANY>
		<PRICE>8.90</PRICE>
		<YEAR>1988</YEAR>
	</CD>
	<CD>
		<TITLE>Bridge of Spies</TITLE>
		<ARTIST>T'Pau</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Siren</COMPANY>
		<PRICE>7.90</PRICE>
		<YEAR>1987</YEAR>
	</CD>
	<CD>
		<TITLE>Private Dancer</TITLE>
		<ARTIST>Tina Turner</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>Capitol</COMPANY>
		<PRICE>8.90</PRICE>
		<YEAR>1983</YEAR>
	</CD>
	<CD>
		<TITLE>Midt om natten</TITLE>
		<ARTIST>Kim Larsen</ARTIST>
		<COUNTRY>EU</COUNTRY>
		<COMPANY>Medley</COMPANY>
		<PRICE>7.80</PRICE>
		<YEAR>1983</YEAR>
	</CD>
	<CD>
		<TITLE>Pavarotti Gala Concert</TITLE>
		<ARTIST>Luciano Pavarotti</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>DECCA</COMPANY>
		<PRICE>9.90</PRICE>
		<YEAR>1991</YEAR>
	</CD>
	<CD>
		<TITLE>The dock of the bay</TITLE>
		<ARTIST>Otis Redding</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>Atlantic</COMPANY>
		<PRICE>7.90</PRICE>
		<YEAR>1987</YEAR>
	</CD>
	<CD>
		<TITLE>Picture book</TITLE>
		<ARTIST>Simply Red</ARTIST>
		<COUNTRY>EU</COUNTRY>
		<COMPANY>Elektra</COMPANY>
		<PRICE>7.20</PRICE>
		<YEAR>1985</YEAR>
	</CD>
	<CD>
		<TITLE>Red</TITLE>
		<ARTIST>The Communards</ARTIST>
		<COUNTRY>UK</COUNTRY>
		<COMPANY>London</COMPANY>
		<PRICE>7.80</PRICE>
		<YEAR>1987</YEAR>
	</CD>
	<CD>
		<TITLE>Unchain my heart</TITLE>
		<ARTIST>Joe Cocker</ARTIST>
		<COUNTRY>USA</COUNTRY>
		<COMPANY>EMI</COMPANY>
		<PRICE>8.20</PRICE>
		<YEAR>1987</YEAR>
	</CD>
</CATALOG>

/**
 * 
 */
package com.ness.xml2json.converter;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;

/**
 * @author PavanKumar Mantha
 * 
 */
public class Xml2Json {

	public void getJsonFromXml(String xml) {
		XMLSerializer xmlSerializer = new XMLSerializer();
		JSON json = xmlSerializer.read(xml);
		System.out.println(json.toString(2));
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		BufferedInputStream bufferedInputStream = null;
		StringBuffer buffer = new StringBuffer();
		try {
			FileInputStream fileInputStream = new FileInputStream(new File("cd_catalog.xml"));
			bufferedInputStream = new BufferedInputStream(fileInputStream);
			int ch;
			while((ch = bufferedInputStream.read()) != -1){
				buffer.append((char)ch);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally{
			try {
				if(bufferedInputStream != null){
					bufferedInputStream.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		String xml = buffer.toString(); 
		//"<?xml version='1.0' encoding='ISO-8859-1' ?> <!--  Edited by XMLSpy® --><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";
		new Xml2Json().getJsonFromXml(xml);
	}
}

[
    {
    "TITLE": "Empire Burlesque",
    "ARTIST": "Bob Dylan",
    "COUNTRY": "USA",
    "COMPANY": "Columbia",
    "PRICE": "10.90",
    "YEAR": "1985"
  },
    {
    "TITLE": "Hide your heart",
    "ARTIST": "Bonnie Tyler",
    "COUNTRY": "UK",
    "COMPANY": "CBS Records",
    "PRICE": "9.90",
    "YEAR": "1988"
  },
    {
    "TITLE": "Greatest Hits",
    "ARTIST": "Dolly Parton",
    "COUNTRY": "USA",
    "COMPANY": "RCA",
    "PRICE": "9.90",
    "YEAR": "1982"
  },
    {
    "TITLE": "Still got the blues",
    "ARTIST": "Gary Moore",
    "COUNTRY": "UK",
    "COMPANY": "Virgin records",
    "PRICE": "10.20",
    "YEAR": "1990"
  },
    {
    "TITLE": "Eros",
    "ARTIST": "Eros Ramazzotti",
    "COUNTRY": "EU",
    "COMPANY": "BMG",
    "PRICE": "9.90",
    "YEAR": "1997"
  },
    {
    "TITLE": "One night only",
    "ARTIST": "Bee Gees",
    "COUNTRY": "UK",
    "COMPANY": "Polydor",
    "PRICE": "10.90",
    "YEAR": "1998"
  },
    {
    "TITLE": "Sylvias Mother",
    "ARTIST": "Dr.Hook",
    "COUNTRY": "UK",
    "COMPANY": "CBS",
    "PRICE": "8.10",
    "YEAR": "1973"
  },
    {
    "TITLE": "Maggie May",
    "ARTIST": "Rod Stewart",
    "COUNTRY": "UK",
    "COMPANY": "Pickwick",
    "PRICE": "8.50",
    "YEAR": "1990"
  },
    {
    "TITLE": "Romanza",
    "ARTIST": "Andrea Bocelli",
    "COUNTRY": "EU",
    "COMPANY": "Polydor",
    "PRICE": "10.80",
    "YEAR": "1996"
  },
    {
    "TITLE": "When a man loves a woman",
    "ARTIST": "Percy Sledge",
    "COUNTRY": "USA",
    "COMPANY": "Atlantic",
    "PRICE": "8.70",
    "YEAR": "1987"
  },
    {
    "TITLE": "Black angel",
    "ARTIST": "Savage Rose",
    "COUNTRY": "EU",
    "COMPANY": "Mega",
    "PRICE": "10.90",
    "YEAR": "1995"
  },
    {
    "TITLE": "1999 Grammy Nominees",
    "ARTIST": "Many",
    "COUNTRY": "USA",
    "COMPANY": "Grammy",
    "PRICE": "10.20",
    "YEAR": "1999"
  },
    {
    "TITLE": "For the good times",
    "ARTIST": "Kenny Rogers",
    "COUNTRY": "UK",
    "COMPANY": "Mucik Master",
    "PRICE": "8.70",
    "YEAR": "1995"
  },
    {
    "TITLE": "Big Willie style",
    "ARTIST": "Will Smith",
    "COUNTRY": "USA",
    "COMPANY": "Columbia",
    "PRICE": "9.90",
    "YEAR": "1997"
  },
    {
    "TITLE": "Tupelo Honey",
    "ARTIST": "Van Morrison",
    "COUNTRY": "UK",
    "COMPANY": "Polydor",
    "PRICE": "8.20",
    "YEAR": "1971"
  },
    {
    "TITLE": "Soulsville",
    "ARTIST": "Jorn Hoel",
    "COUNTRY": "Norway",
    "COMPANY": "WEA",
    "PRICE": "7.90",
    "YEAR": "1996"
  },
    {
    "TITLE": "The very best of",
    "ARTIST": "Cat Stevens",
    "COUNTRY": "UK",
    "COMPANY": "Island",
    "PRICE": "8.90",
    "YEAR": "1990"
  },
    {
    "TITLE": "Stop",
    "ARTIST": "Sam Brown",
    "COUNTRY": "UK",
    "COMPANY": "A and M",
    "PRICE": "8.90",
    "YEAR": "1988"
  },
    {
    "TITLE": "Bridge of Spies",
    "ARTIST": "T'Pau",
    "COUNTRY": "UK",
    "COMPANY": "Siren",
    "PRICE": "7.90",
    "YEAR": "1987"
  },
    {
    "TITLE": "Private Dancer",
    "ARTIST": "Tina Turner",
    "COUNTRY": "UK",
    "COMPANY": "Capitol",
    "PRICE": "8.90",
    "YEAR": "1983"
  },
    {
    "TITLE": "Midt om natten",
    "ARTIST": "Kim Larsen",
    "COUNTRY": "EU",
    "COMPANY": "Medley",
    "PRICE": "7.80",
    "YEAR": "1983"
  },
    {
    "TITLE": "Pavarotti Gala Concert",
    "ARTIST": "Luciano Pavarotti",
    "COUNTRY": "UK",
    "COMPANY": "DECCA",
    "PRICE": "9.90",
    "YEAR": "1991"
  },
    {
    "TITLE": "The dock of the bay",
    "ARTIST": "Otis Redding",
    "COUNTRY": "USA",
    "COMPANY": "Atlantic",
    "PRICE": "7.90",
    "YEAR": "1987"
  },
    {
    "TITLE": "Picture book",
    "ARTIST": "Simply Red",
    "COUNTRY": "EU",
    "COMPANY": "Elektra",
    "PRICE": "7.20",
    "YEAR": "1985"
  },
    {
    "TITLE": "Red",
    "ARTIST": "The Communards",
    "COUNTRY": "UK",
    "COMPANY": "London",
    "PRICE": "7.80",
    "YEAR": "1987"
  },
    {
    "TITLE": "Unchain my heart",
    "ARTIST": "Joe Cocker",
    "COUNTRY": "USA",
    "COMPANY": "EMI",
    "PRICE": "8.20",
    "YEAR": "1987"
  }
]
Advertisements

AngularJS a modern JavaScript

Hi all, i was doing some weekend research on the modern JavaScript frameworks and i felt there are lot many awesome features in the latest JavaScript frameworks. so now i wanted to start a series of articles which describe Angular in its vanilla flavour. Any one with basic Javascript, HTML background can start working in AngularJS without any hassle.

Let us dive into the world of AngularJS and see what it is.
AngularJS-icon

AngularJS is an open-source JavaScript framework, developed by Google . Its main aim is to incorporate browser-based applications with model–view–controller (MVC) capability, in an effort to make both development and testing easier. AngularJS lets you extend HTML vocabulary for your application. The resulting environment is extraordinarily expressive, readable, and quick to develop.

google maintain Angular CDN at:

<script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>

<!DOCTYPE html>
<html ng-app>
<head>
<meta charset="ISO-8859-1">
<title>Angular JS Tutorial</title>
<script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
</head>
<body>
	 Write some text in textbox:
    <input type="text" ng-model="sometext" />
 
    <h1>Hello {{ sometext }}</h1>
</body>
</html>

please run this and u will notice that view and model are bound together, when ever u change the model value, the view gets automatically changes.

here is the online demo: Demo

Happy AngularJS 🙂