java - 在 Java 中将 XML 与 JSON 相互转换(无需额外的 <e> 和 <o> 元素)

标签 java xml json

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

有没有办法避免 <e><a>节点,是生成的?这会被路径表达式破坏得很不方便吗?

考虑下面的例子:

{"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 中似乎不可用。

最佳答案

您可以使用StAXON来做到这一点- 通过 StAX 的 JSON。 StAXON 实现了 XML 的流 API,但读取和写入 JSON。也就是说,您可以使用标准 XML 工具来完成这项工作。

复制 XML 的常见“技巧”是使用身份 XML 转换 (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();
}

使用您的示例运行此命令会产生:

<?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 可以使用这些。将此设置保留为 false如果您不需要返回 JSON。

关于java - 在 Java 中将 XML 与 JSON 相互转换(无需额外的 <e> 和 <o> 元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7472282/

相关文章:

python - 将带有 unicode 字符的 XML 解析为 ElementTree

java - 找不到 Maven 注释处理处理器

java - 在 Java 中使用 final 关键字会提高性能吗?

java - 尝试从 JSP 访问 Action 方法时 struts2 NoSuchMethodException

python - lxml(或 lxml.html): print tree structure

php - 使用 php 在 json 中搜索

java - 如何在 Struts 2 中的一个表单上添加两个提交按钮

java - 向字符串添加转义字符的简单方法

sql - 如何将 JSON 映射到 SQL 架构?

c# - MVC JsonResult 方法不接受参数