JSON4J XML usage examples

The XML converter is straightforward to use.  The converter provides a simple static helper class where you can provide an XML document as a stream or as a file on disk.  The method of output is also selectable; you can stream the resulting JavaScriptTM Object Notation (JSON) to an output stream or obtain it as a JavaTM String.  You can also specify how verbose to make the generated JSON.  The default is a compact form with  no indentions or new lines, which is the most efficient format for transmission.  Verbose form is indented and spaced, which is a more readable format and is the mode to enable when you want to debug and examine the JSON structure generated.  See the following examples that demonstrate how some basic XML element formats are converted to JSON formats:

<getValuesReturn return="true">
   <attribute attrValue="value"/>
   <String>First item</String>
   <String>Second item</String>
   <String>Third item</String>
   <TextTag>Text!</TextTag>
   <EmptyTag/>
   <TagWithAttrs attr1="value1" attr2="value2" attr3="value3"/>
   <TagWithAttrsAndText attr1="value1" attr2="value2" attr3="value3">Text!</TagWithAttrsAndText>
</getValuesReturn>

Conversion to JSON (verbose form):
{
   "getValuesReturn" : {
      "return" : "true",
      "TextTag" : "Text!",
      "String" : [
         "First item",
         "Second item",
         "Third item"
      ],
      "TagWithAttrsAndText" : {
         "content" : "Text!",
         "attr3" : "value3",
         "attr2" : "value2",
         "attr1" : "value1"
      }
      ,
      "EmptyTag" : true,
      "attribute" : {
         "attrValue" : "value"
      }
      ,
      "TagWithAttrs" : {
         "attr3" : "value3",
         "attr2" : "value2",
         "attr1" : "value1"
      }
   }
}

Terms of Use | Feedback