开发者

Convert XML to/from JSON in Java (without extra <e> and <o> elements)

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>
        <pri开发者_运维百科ce 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:

InputStream input = ... // your JSON input;
OutputStream output = System.out;
try {
    /*
     * Create source.
     */
    XMLInputFactory inputFactory = new JsonXMLInputFactory();
    inputFactory.setProperty(JsonXMLInputFactory.PROP_MULTIPLE_PI, false);
    Source source = new StAXSource(inputFactory.createXMLStreamReader(input));

    /*
     * Create result.
     */
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(output);
    writer = new PrettyXMLStreamWriter(writer); // format output
    Result result = new StAXResult(writer);

    /*
     * Transform (copy).
     */
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.newTransformer().transform(source, result);
} finally {
    /*
     * As per StAX specification, XMLStreamReader/Writer.close() doesn't close
     * the underlying stream.
     */
    output.close();
    input.close();
}

Running this with your example produces:

<?xml version="1.0" ?>
<store>
    <book>
        <category>reference</category>
        <author>Nigel Rees</author>
        <title>Sayings of the Century</title>
        <price>8.95</price>
    </book>
    <book>
        <category>fiction</category>
        <author>Evelyn Waugh</author>
        <title>Sword of Honour</title>
        <price>12.99</price>
    </book>
    <book>
        <category>fiction</category>
        <author>Herman Melville</author>
        <title>Moby Dick</title>
        <isbn>0-553-21311-3</isbn>
        <price>8.99</price>
    </book>
    <book>
        <category>fiction</category>
        <author>J. R. R. Tolkien</author>
        <title>The Lord of the Rings</title>
        <isbn>0-395-19395-8</isbn>
        <price>22.99</price>
    </book>
    <bicycle>
        <color>red</color>
        <price>19.95</price>
    </bicycle>
</store>

Note: If the output property PROP_MULTIPLE_PI is set to true, 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 to false if you don't need to go back to JSON.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜