Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package edu.ucsd.ccdb.cil.xml2json; import edu.ucsd.ccdb.cil.xml2json.database.CCDBXMLResource; import java.io.*; import java.net.*; import org.json.*; /** * * @author ncmir */ public class XML2JsonUtil { public static int PRETTY_PRINT_INDENT_FACTOR = 4; public String getTextFromURL(String url) throws Exception { URL website = new URL(url); URLConnection connection = website.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); String inputLine; while ((inputLine = in.readLine()) != null) response.append(inputLine + "\n"); in.close(); return response.toString(); } public String xml2Json(String xml) throws Exception { String jsonPrettyPrintString = ""; try { JSONObject xmlJSONObj = XML.toJSONObject(xml); jsonPrettyPrintString = xmlJSONObj.toString(); //System.out.println(jsonPrettyPrintString); } catch (JSONException je) { System.out.println(je.toString()); } return jsonPrettyPrintString; } public void outputStringToFile(File f, String json) throws Exception { FileOutputStream fout = new FileOutputStream(f); PrintWriter pw = new PrintWriter(fout); pw.print(json); pw.close(); } public static void main(String[] args) throws Exception { XML2JsonUtil xutil = new XML2JsonUtil(); //String xml= xutil.getTextFromURL("http://www.cellimagelibrary.org/ccdbXML/3573.xml"); //System.out.println(xml); CCDBXMLResource resource = new CCDBXMLResource(); String xml = resource.getXMLContent(new File("/Users/ncmir/Desktop/CCDB/ccdbXML2/3573.xml")); String json = xutil.xml2Json(xml); File f = new File(Constants.jsonOutputFolder2 + "/" + "3573.json"); xutil.outputStringToFile(f, json); } }