在Java中将XML转换为JSON/从JSON转换为XML(没有额外的< e>和< o>元素) [英] Convert XML to/from JSON in Java (without extra <e> and <o> elements)

查看:80
本文介绍了在Java中将XML转换为JSON/从JSON转换为XML(没有额外的< e>和< o>元素)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用json-lib库中的XMLSerializer,以便在JSON和XML之间进行转换.

I am using the XMLSerializer found in the json-lib library, in order to transform between JSON and XML and back.

是否仍然要避免生成的<e><a>节点?这很不方便被路径表达式破坏了?

Is there anyway to avoid the <e> and <a> nodes, that are generated? This is very inconveniently ruining by path-expressions?

考虑以下示例:

{"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
  }
}}

序列化为:

<?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>

类似这样的东西 http://jsontoxml.utilities-online.info/ 提供的正是我想要的,但似乎在Java中不可用.

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.

推荐答案

您可以使用 StAXON -通过StAX的JSON. StAXON实现XML的Streaming API,但读写JSON.也就是说,您可以使用标准的XML工具来完成作业.

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.

复制XML的常见技巧"是使用身份XML转换(XSLT).

A common "trick" to copy XML is using an identity XML transformation (XSLT).

以下是一些代码:

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>

注意:如果输出属性PROP_MULTIPLE_PI设置为true,StAXON将生成<xml-multiple>处理指令.当转换回JSON来触发数组启动时,StAXON可以使用它们.如果您不需要返回JSON,则将此设置保留为false.

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.

这篇关于在Java中将XML转换为JSON/从JSON转换为XML(没有额外的&lt; e&gt;和&lt; o&gt;元素)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆