title: Convert xml to json in java
body: <p>I want to convert from xml to json in java and get an output.
  But whike doing so only some parts from the xml are getting converted and not the whole xml.
  Any help
  My input xml is  :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;important-data certified="true" processed="true"&gt;
      &lt;timestamp&gt;232423423423&lt;/timestamp&gt;
          &lt;authors&gt;
              &lt;author&gt;
                  &lt;firstName&gt;Tim&lt;/firstName&gt;
                  &lt;lastName&gt;Leary&lt;/lastName&gt;
              &lt;/author&gt;
          &lt;/authors&gt;
          &lt;title&gt;Flashbacks&lt;/title&gt;
          &lt;shippingWeight&gt;1.4 pounds&lt;/shippingWeight&gt;
          &lt;isbn&gt;978-0874778700&lt;/isbn&gt;
  &lt;/important-data&gt;
  </code></pre>
  
  <p>And my code is:
  package com.discursive.answers;</p>
  
  <pre><code>import java.io.IOException;
  import java.io.InputStream;
  
  import org.apache.commons.io.IOUtils;
  
  import net.sf.json.JSON;
  import net.sf.json.JSONException;
  import net.sf.json.JSONObject;
  import net.sf.json.xml.XMLSerializer;
  import org.json.XML;
  
  public class ConvertXMLtoJSON {
  public static int PRETTY_PRINT_INDENT_FACTOR = 4;
  public static String TEST_XML_STRING = "&lt;?xml version=\"1.0\" ?&gt;" +
  "&lt;test attrib=\"moretest\"&gt;" +
  "Turn this to JSON&lt;/test&gt;";
  
  public static void main(String[] args) {
      new ConvertXMLtoJSON();
  }
  
  public ConvertXMLtoJSON() {
      try {
          InputStream is = ConvertXMLtoJSON.class.getResourceAsStream("sample-xml.xml");
          if (is != null) {
              String xml = IOUtils.toString(is);
              JSON json = XMLSerializer.readObject(xml);
  
              System.out.println(json.toString().split(",").length);
              for(int i= 0 ;i &lt; json.toString().split(",").length; i ++)
                  System.out.println(json.toString().split(",")[i]);
          } else {
              System.out.println("Checkpoint 1");
          }
      } catch (java.io.IOException ioe) {
          ioe.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Applying the above mentioned xml and code I get the output as:</p>
  
  <pre><code>{"timestamp":"232423423423"
  "authors":"\n\t\t\n\t\t\tTim\n\t\t\tLeary\n\t\t\n\t"
  "title":"Flashbacks"
  "isbn":"978-0874778700"
  "shippingWeight":"1.4 pounds"}
  </code></pre>
  
  <p>where as it should be as :</p>
  
  <pre><code>  { 
   "@certified": "true", 
   "@processed": "true", 
   "timestamp": "232423423423", 
   "authors": [  {   
   "firstName": "Tim",   
   "lastName": "Leary" 
   }], 
   "title":
   "Flashbacks", 
   "shippingWeight": "1.4 pounds", 
   "isbn": "978-0874778700"
  }
  </code></pre>
  
  <p>What changes should I do to get the required output?</p>