Home | Projects | Tutorials | Articles | live chat | Submit Project | Big Vote
 
Ajax Projects
.NET Frameworks
Java Frameworks
PHP Frameworks
Ruby Frameworks
Other Frameworks
Cool AJAX sites
Ajax Resources
Ajax Tools
JavaScript frameworks

 Home /  Tutorials / Convert XML To JSON Using Java

Convert XML To JSON Using Java





Here you can know how to add JSON support to any Java program. and it can convert XML into JSON object.first you can add this Dependency to your Java Project:

Read The Full Tutorial.
































Here you can know how to add JSON support to any Java program. and it can convert XML into JSON object.first you can add this Dependency to your Java Project:

<dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.3</version>
        <type>jar</type>
        <classifier>jdk15</classifier>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>xom</groupId>
        <artifactId>xom</artifactId>
        <version>1.1</version>
    </dependency>
  </dependencies>

- Now you can add this XML in your classpath in a file named sample-xml.xml:

<?xml version="1.0" encoding="UTF-8"?>
<important-data certified="true" processed="true">
  <timestamp>232423423423</timestamp>
  <authors>
    <author>
      <firstName>Tim</firstName>
      <lastName>Leary</lastName>
    </author>
  </authors>
  <title>Flashbacks</title>
  <shippingWeight>1.4 pounds</shippingWeight>
  <isbn>978-0874778700</isbn>
</important-data>

Load the resource from the classpath and parse this XML into JSON as follows:

package com.discursive.answers;

import java.io.InputStream;

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

import org.apache.commons.io.IOUtils;

public class ConvertXMLtoJSON {

        public static void main(String[] args) throws Exception {
                InputStream is =
                        ConvertXMLtoJSON.class.getResourceAsStream("sample-xml.xml");
                String xml = IOUtils.toString(is);
               
                XMLSerializer xmlSerializer = new XMLSerializer();
                JSON json = xmlSerializer.read( xml ); 
                System.out.println( json.toString(2) );
        }
}

 This should produce the following JSON output:

{
  "@certified": "true",
  "@processed": "true",
  "timestamp": "232423423423",
  "authors": [  {
    "firstName": "Tim",
    "lastName": "Leary"
  }],
  "title": "Flashbacks",
  "shippingWeight": "1.4 pounds",
  "isbn": "978-0874778700"
}

AddThis Social Bookmark Button
Top Projects
MSN Web Messenger
MessengerFX
ebuddy
e-messenger
ILoveIM
You Tube
AJAX file upload
KoolIM.com
Meebo
Ajax.NET Professional
Tutorials
Playing Accordion With CSS and jQuery
How to Use JSON Class in Flex
AJAX without refreshing the chat room to achieve
Optimize ASP.NET Membership Stored Procedures for greater speed and scalability
Getting started with AJAX using PHP
Fancy Quotes With jQuery, AJAX & CSS
Fill Dropdown Using Ajax
jQuery AJAX Tabs with ThickBox enabled
Using jQuery to enhance ASP.NET AJAX progress indication
Creating a dropdown menu with jQuery and CSS