Table of Contents |
---|
minLevel | 1 |
---|
maxLevel | 2 |
---|
outline | false |
---|
type | list |
---|
printable | false |
---|
|
...
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: | Invalid (single root element is not allowed by default by Jackson) | |
Root element with text: Code Block |
---|
| <root>Some text</root> |
| Invalid (single root element with text is not allowed by default by Jackson) | |
Nested element: Code Block |
---|
| <root>
<orders/>
</root> |
| Code Block |
---|
| {
"orders": ""
} |
| |
Nested element with text: Code Block |
---|
| <root>
<orders>Some text</orders>
</root> |
| Code Block |
---|
| {
"orders": "Some text"
} |
| Code Block |
---|
| orders
-> "Some text" |
|
Deeper nested element with text: Code Block |
---|
| <root>
<orders>
<item>My Item</item>
</orders>
</root> |
| Code Block |
---|
| {
"orders": {
"item": "My Item"
}
} |
| Code Block |
---|
| orders.item
-> "My Item" |
|
Multiple deep elements of same name: Code Block |
---|
| <root>
<orders>
<item>My Item1</item>
<item>My Item2</item>
</orders>
</root> |
| Code Block |
---|
| {
"orders": {
"item": [
"My Item1",
"My Item2"
]
}
} |
| Code Block |
---|
| 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 |
---|
| <root>
<orders>
<itemA>Item1</itemA>
<itemB>Item2</itemB>
</orders>
</root> |
| Code Block |
---|
| {
"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 |
---|
| <root>
<orders>
<item id="0">Text</item>
</orders>
</root> |
| Code Block |
---|
| {
"orders": {
"item": {
"id": "0",
"": "Text"
}
}
} |
| Code Block |
---|
orders.item.id
-> "0" |
Code Block |
---|
orders.item['']
-> "Text" |
|
Mixed content (text + element): Code Block |
---|
| <root>
text
<a>Foo</a>
</root> |
| Code Block |
---|
| {
"": "\n text\n ",
"a": "Foo"
} |
| Code Block |
---|
['']
-> "\n text\n" |
|
...