DURGA DURGA - 4 months ago 87
JSON Question

How to convert xml to .json file in java

public class ConvertXMLtoJSON {

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


but i am getting error

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1020)
at org.apache.commons.io.IOUtils.toString(IOUtils.java:358)
at com.apache.poi.ConvertXMLtoJSON.main(ConvertXMLtoJSON.java:13


can u please help me to resolve it
This is my xml format


ac3
AC3 Phone
ACME
phone
200.0
1.0
true
























i have generated this xml from my excel file and i have convert this xml file to json file

Answer

Hi Anil This is the code which is used to convert xml to json

 import org.json.JSONObject;  
  import org.json.JSONException;  
 import org.json.XML;  
 import java.io.*;  
 public class ConvertXMLtoJSON2{  
      public static void main(String[] args) throws Exception   
    {  
        String fileName = "D:\\temp.json";
        try  
        {  


            File file = new File ("D:\\output333.xml");  
            InputStream inputStream = new FileInputStream(file);  
            StringBuilder builder =  new StringBuilder();  
            int ptr = 0;  
            while ((ptr = inputStream.read()) != -1 )  
            {  
                builder.append((char) ptr); 
              //  System.out.println(ptr);
            }  

            String xml  = builder.toString();  
            JSONObject jsonObj = XML.toJSONObject(xml);   
           // System.out.println(jsonObj.toString()); 
           // System.out.println(jsonObj.toString().split(",").length);
         // Assume default encoding.
            FileWriter fileWriter =
                new FileWriter(fileName);

            // Always wrap FileWriter in BufferedWriter.
            BufferedWriter bufferedWriter =
                new BufferedWriter(fileWriter);

            // Always close files.


            for(int i= 0 ;i < jsonObj.toString().split(",").length; i ++) {
               System.out.println(jsonObj.toString().split(",")[i]);
               bufferedWriter.write(jsonObj.toString().split(",")[i]);
               bufferedWriter.write("\n");
            }

            bufferedWriter.close();



        }


            /* 
            String xmlString  = "<?xml version=\"1.0\"?><ASF_Service_ResponseVO id=\"1\"><service type=\"String\">OnboardingV2</service><operation type=\"String\">start_onboarding_session</operation><requested_version type=\"String\">1.0</requested_version><actual_version type=\"String\">1.0</actual_version><server_info type=\"String\">onboardingv2serv:start_onboarding_session&CalThreadId=85&TopLevelTxnStartTime=13b40fe91c4&Host=L-BLR-00438534&pid=3564</server_info><result type=\"Onboarding::StartOnboardingSessionResponse\" id=\"2\"><onboarding_id type=\"String\">137</onboarding_id><success type=\"bool\">true</success></result></ASF_Service_ResponseVO>"; 

            JSONObject jsonObj = XML.toJSONObject(xmlString);  
            System.out.println(jsonObj.toString());  
            */
          catch(IOException ex) {
                System.out.println(
                    "Error writing to file '"
                    + fileName + "'");
                // Or we could just do this:
                // ex.printStackTrace();
            }catch(Exception e)  
            {  
                e.printStackTrace();  
            }
    }  

}