Converting complex XML with repeating elements to JSON in the same order
<p>First let me confess that I'm a beginner with little knowledge of Java. I'm facing a problem with XML to JSON conversion. The XML that I need to covert is complex with elements that will repeat throughout the document. And the order in which the elements appear need to be preserved too.
Sample XML:</p>

<pre><code>   &lt;books&gt;
     &lt;book&gt;
       &lt;chapter&gt;
         &lt;number&gt;i&lt;/number&gt;
         &lt;title&gt;Introduction&lt;/title&gt;
       &lt;/chapter&gt;
       &lt;volume&gt;
         &lt;title&gt;this title contains &lt;italic&gt;markup&lt;/italic&gt; tags&lt;/title&gt;
         &lt;part&gt;
           &lt;title&gt;part may contain sections and nested parts&lt;/title&gt;
           &lt;section&gt;
              &lt;title&gt;contains chapters&lt;/title&gt;
              &lt;chapter&gt;
                &lt;number&gt;1.1.1&lt;/number&gt;
                &lt;title&gt;first chapter&lt;/title&gt;
              &lt;/chapter&gt;
           &lt;/section&gt;
         &lt;/part&gt;
         &lt;chapter&gt;
           &lt;number&gt;x&lt;/number&gt;
           &lt;title&gt;last chapter in volume&lt;/title&gt;
         &lt;/chapter&gt;
       &lt;/volume&gt;
       &lt;chapter&gt;
          &lt;number&gt;xii&lt;/number&gt;
          &lt;title&gt;last chapter in book&lt;/title&gt;
       &lt;/chapter&gt;
     &lt;/book&gt;
    &lt;/books&gt;
</code></pre>

<p>I'm trying to build a table of contents kind of page and I need this info in JSON format. Since the order is important here I decided that the structure of the resulting JSON should be such that all elements are transformed to similar structure in JSON.</p>

<p>Expected JSON:</p>

<pre><code>    {
      "Book": [
        {
          "Type": "chapter",
          "Title": "Introduction",
          "Number": "i",
          "List": ""
        },
        {
          "Type": "volume",
          "Title": "this title contains &lt;![CDATA[&lt;span style='italic'&gt;markup&lt;/span&gt;]]&gt; tags",
          "Number": "",
          "List": [
            {
              "Type": "part",
              "Title": "part may contain sections and nested parts",
              "Number":"",
              "List": [
                {
                  "Type":"section",
                  "Title":"contains chapters",
                  "Number":"",
                  "List":[
                    {
                      "Type":"chapter",
                      "Title":"first chapter",
                      "Number":"1.1.1",
                      "List":""
                    }
                  ]
                }
                ]
            },
            {
              "Type":"chapter",
              "Title":"last chapter in volume",
              "Number":"x",
              "List":""
            }
          ]
        },
        {
          "Type": "chapter",
          "Title": "last chapter in book",
          "Number": "xii",
          "List": ""
        }
      ]
    }    
</code></pre>

<p>Two more problems that I'm facing are the data type conversion that occurs when using most JSON libraries like 'org.json'. And the last requirement is that if there are markup tags like <code>&lt;italic&gt;</code> inside elements then it should be wrapped within CDATA. </p>

<p>I tried the usual ways like org.json <code>XML.toJSONObject()</code> which (as expected) didn't do the trick. Next I tried converting XML to POJO and converting POJO to XML using JAXB. But this also proved ineffective as the structure was lost when the XML is unmarshalled into object. I also tried Staxon but the resulting JSON, even though it retained the original structure, wasn't valid. </p>

<p>Is there a way except using XSLT, to do this conversion? E.g., when using JAXB to convert to POJO, is it possible that the use of LinkedHashMaps when unmarshalling will maintain the structure?</p>

<p>Is DOM/SAX the correct way to do this complex transformation? I really don't want to use XSLT to convert XML to JSON as I've faced many issues with this.</p>