I am using the XMLSerializer found in the json-lib library, in order to transform between JSON and XML and back.
Is there anyway to avoid the <e> and <a> nodes, that are generated? This is very inconveniently ruining by path-expressions?
Consider the example below:
{"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}}
Is serialised into:
<?xml version="1.0" encoding="UTF-8"?>
<o>
<store class="object">
<bicycle class="object">
<color type="string">red</color>
<price type="number">19.95</price>
</bicycle>
<book class="array">
<e class="object">
<author type="string">Nigel Rees</author>
<category type="string">reference</category>
<price type="number">8.95</price>
<title type="string">Sayings of the Century</title>
</e>
<e class="object">
<author type="string">Evelyn Waugh</author>
<category type="string">fiction</category>
<price type="number">12.99</price>
<title type="string">Sword of Honour</title>
</e>
<e class="object">
<author type="string">Herman Melville</author>
<category type="string">fiction</category>
<isbn type="string">0-553-21311-3</isbn>
<price type="number">8.99</price>
<title type="string">Moby Dick</title>
</e>
<e class="object">
<author type="string">J. R. R. Tolkien</author>
<category type="string">fiction</category>
<isbn type="string">0-395-19395-8</isbn>
<price type="number">22.99</price>
<title type="string">The Lord of the Rings</title>
</e>
</book>
</store>
</o>
Something like what this http://jsontoxml.utilities-online.info/ provides would have been exactly what I’m after, but don’t seem to be available in Java.
You can do this with StAXON – JSON via StAX. StAXON implements the Streaming API for XML, but reads and writes JSON. That is, you can use standard XML tools to do the Job.
A common “trick” to copy XML is using an identity XML transformation (XSLT).
Here’s some code:
Running this with your example produces:
Note: If the output property
PROP_MULTIPLE_PIis set totrue, StAXON will generate<xml-multiple>processing instructions. These can be used by StAXON when converting back to JSON to trigger array starts. Leave this set tofalseif you don’t need to go back to JSON.