Showing posts with label J2EE. Show all posts
Showing posts with label J2EE. Show all posts

Wednesday, June 10, 2015

Intellij IDEA for Students FREE (1 Year)

Folks,
          Jetbrains is offering Intellij IDEA Ultimate Edition for one year FREE for students with edu email address. If you are student and you got .edu email address go and download the most intelligent IDE.

                            https://www.jetbrains.com/estore/students/


Happy Coding!

Friday, April 3, 2015

XML To JSON Converter

Have the following jars in the classpath,

 commons-beanutils-1.6.1.jar
 commons-collections-3.2.jar
 commons-lang-2.1.jar
 commons-logging-1.1.jar
 ezmorph-1.0.4.jar
 json-lib-2.3-jdk15.jar
 xom-1.0.jar

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

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class XML2JSONConverter {
    public String convertXMLTOJSON(String inputXML) {
        String jsonString = "";
        try {
            JSON objJson = new XMLSerializer().read(inputXML);
            jsonString = objJson.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonString;
    }

    static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }

    public static void main(String[] args) throws Exception {
        File inputFile = new File("input xml file path");
        InputStream inputStreams = new FileInputStream(inputFile);
        XML2JSONConverter toJASON = new XML2JSONConverter();
        System.out.println("JASON Object:" + toJASON.convertXMLTOJSON(convertStreamToString(inputStreams)));
    }
}

Java InputStream to String Covertion

Found this small method to convert InputStream to String, This will help to avoid using Apache IOUtils etc.,

 static String convertStreamToString(java.io.InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }