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

查看:22
本文介绍了在 Java 中将 XML 与 JSON 相互转换(没有额外的 <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.

无论如何要避免生成的 节点?这很不方便被路径表达式破坏?

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将生成处理指令.当转换回 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 相互转换(没有额外的 &amp;lt;e&gt; 和 &amp;lt;o&amp;gt; 元素)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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