Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents
minLevel1
maxLevel2
outlinefalse
typelist
printablefalse

...

This will use the Jackson XML Mapping scheme to convert a given XML to JSON. It creates a much easier to use JSON structure but doesn’t support all XML features as the default conversion scheme does. Also conversion back from JSON to XML is not straight forward. But for one-way conversion XML → JSON with simple structures it is a good choice.

The jackson conversion scheme is described in this table:

XML

JSON

Access with PEL (Examples)

Single root element:

Code Block
languagexml
<root/>

Invalid (single root element is not allowed by default by Jackson)

Root element with text:

Code Block
languagexml
<root>Some text</root>

Invalid (single root element with text is not allowed by default by Jackson)

Nested element:

Code Block
languagexml
<root>
  <orders/>
</root>

Code Block
languagejson
{
  "orders": ""
}

Code Block
languagenone
orders   
-> "" 

Nested element with text:

Code Block
languagexml
<root>
 <orders>Some text</orders>
</root>

Code Block
languagejson
{
  "orders": "Some text"
}

Code Block
languagenone
orders   
-> "Some text" 

Deeper nested element with text:

Code Block
languagexml
<root>
 <orders>
  <item>My Item</item>
 </orders>
</root>

Code Block
languagejson
{
  "orders": {
    "item": "My Item"
  }
}

Code Block
languagenone
orders.item
-> "My Item"   

Multiple deep elements of same name:

Code Block
languagexml
<root>
 <orders>
  <item>My Item1</item>
  <item>My Item2</item>
 </orders>
</root>

Code Block
languagejson
{
  "orders": {
    "item": [
      "My Item1",
      "My Item2"
    ]
  }
}

Code Block
languagenone
orders.item[0]
-> "My Item1"
Code Block
orders['item'][1]
-> "My Item2"
Code Block
orders.item
-> [
    "My Item1", 
    "My Item2"
   ]

Multiple elements of different name:

Code Block
languagexml
<root>
 <orders>
  <itemA>Item1</itemA>
  <itemB>Item2</itemB>
 </orders>
</root>

Code Block
languagejson
{
  "orders": {
    "itemA": "Item1",
    "itemB": "Item2"
  }
}

Code Block
orders.itemA
-> "Item1"
Code Block
orders['itemB']
-> "Item2"
Code Block
orders
-> {
"itemA": "Item1",
"itemB": "Item2"
}

XML attribute and text mixed:

Code Block
languagexml
<root>
 <orders>
  <item id="0">Text</item>
 </orders>
</root>

Code Block
languagejson
{
  "orders": {
    "item": {
      "id": "0",
      "": "Text"
    }
  }
}

Code Block
orders.item.id
-> "0"
Code Block
orders.item['']
-> "Text"

Mixed content (text + element):

Code Block
languagexml
<root>
  text
  <a>Foo</a>
</root>

Code Block
languagejson
{
  "": "\n text\n ",
  "a": "Foo"
}

Code Block
['']
-> "\n text\n"
Code Block
a
-> "Foo"

...