Convert XML to JSON Using Jackson – 使用Jackson将XML转换为JSON

最后修改: 2018年 10月 18日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll see how to convert an XML message to JSON using Jackson.

在本教程中,我们将看到如何使用Jackson将一个XML信息转换为JSON。

For readers new to Jackson, consider getting familiar with the basics first.

对于刚接触杰克逊的读者,可以考虑先熟悉一下基础知识

2. An Introduction to Jackson

2.Jackson的介绍

We can think about parsing JSON in three different ways with Jackson:

我们可以考虑用Jackson的三种不同方式来解析JSON。

Now, Jackson also supports the first two for XML data. As such, let’s see how Jackson can help us do the conversion from one format to the other.

现在,Jackson也支持前两种的XML数据。因此,让我们看看 Jackson 如何帮助我们完成从一种格式到另一种格式的转换。

3. Dependencies

3.依赖性

First, we need to add the jackson-databind dependency to our pom.xml:

首先,我们需要将jackson-databind依赖性添加到我们的 pom.xml

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.3</version>
</dependency>

This library is going to allow us to use the data binding API.

这个库将使我们能够使用数据绑定API。

The second is jackson-dataformat-xml which adds Jackson’s XML support:

第二个是jackson-dataformat-xml,增加了Jackson的XML支持。

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
    <version>2.13.3</version>
</dependency>

4. Data Binding

4.数据绑定

Data binding, simply put, is when we want to map serialized data directly to a Java object.

数据绑定,简单地说,就是当我们想把序列化的数据直接映射到一个Java对象时。

To explore this, let’s define our XML with Flower and Color properties:

为了探索这一点,让我们用FlowerColor属性定义我们的XML

<Flower>
    <name>Poppy</name>
    <color>RED</color>
    <petals>9</petals>
</Flower>

This is similar to this Java notation:

这类似于这个Java符号。

public class Flower {
    private String name;
    private Color color;
    private Integer petals;
    // getters and setters
}

public enum Color { PINK, BLUE, YELLOW, RED; }

Our first step will be to parse the XML into a Flower instance. To do that, let’s create an instance of XmlMapper, Jackson’s XML equivalent for ObjectMapper and use its readValue method:

我们的第一步将是把XML解析成一个Flower实例。要做到这一点,让我们创建一个XmlMapper的实例,Jackson的XML相当于ObjectMapper,并使用其readValue方法。

XmlMapper xmlMapper = new XmlMapper();
Flower poppy = xmlMapper.readValue(xml, Flower.class);

Once we have our Flower instance, we’ll want to write it out as JSON using the familiar ObjectMapper:

一旦我们有了我们的Flower实例,我们就想使用熟悉的ObjectMapper将其写成JSON格式:

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(poppy);

And, as a result, we get our JSON equivalent:

结果是,我们得到了我们的JSON等价物。

{
    "name":"Poppy",
    "color":"RED",
    "petals":9
}

5. Tree Traversal

5.树形遍历

At times, looking directly at the tree structure can offer more flexibility, like in the case that we don’t want to maintain an intermediary class or we only want to convert a portion of the structure.

有时,直接查看树状结构可以提供更多的灵活性,比如在我们不想维护一个中间类或者我们只想转换结构的一部分的情况下。

Though, as we’ll see, it comes with some tradeoffs.

不过,正如我们将看到的那样,它也有一些折衷之处。

The first step is similar to our first step when we use data binding. This time, though, we’ll use the readTree method:

第一步与我们使用数据绑定时的第一步相似。不过这一次,我们将使用readTree方法。

XmlMapper xmlMapper = new XmlMapper();
JsonNode node = xmlMapper.readTree(xml.getBytes());

Having done this, we’ll have a JsonNode which has 3 children, as we expected: name, color, and petals.

做完这些后,我们将有一个JsonNode,它有3个孩子,正如我们所期望的。名字,颜色,花瓣

Then, we can again use ObjectMapper, just sending our JsonNode instead:

然后,我们可以再次使用ObjectMapper,只是发送我们的JsonNode代替。

ObjectMapper jsonMapper = new ObjectMapper();
String json = jsonMapper.writeValueAsString(node);

Now, the outcome is slightly different compared to our last example:

现在,与我们上一个例子相比,结果略有不同:

{
    "name":"Poppy",
    "color":"RED",
    "petals":"9"
}

Upon careful inspection, we can see that the petals attribute is serialized into a string instead of a number! This is because readTree doesn’t infer the datatype without an explicit definition.

仔细观察,我们可以看到花瓣属性被序列化为一个字符串,而不是一个数字! 这是因为readTree在没有明确定义的情况下不会推断数据类型。

5.1. Limitations

5.1.局限性

And, there are certain limitations with Jackson’s XML tree traversal support:

而且,Jackson的XML树形遍历支持存在一定的局限性。

  • Jackson cannot differentiate between an Object and an Array. Since XML lacks native structures to distinguish an object from a list of objects, Jackson will simply collate repeated elements into a single value.
  • And, since Jackson wants to map each XML element to a JSON node, it doesn’t support mixed content.

For these reasons, the official Jackson docs recommend against using Tree models to parse XML.

由于这些原因,Jackson 的官方文档建议不要使用树模型来解析 XML

6. Memory Constraints

6.内存限制

Now, both of these have the notable downside that the entire XML needs to be in memory at once in order to perform the conversion. Until Jackson supports streaming the tree structure as tokens, we’ll be stuck with this constraint or we’ll need to take a look at rolling our own with something like XMLStreamReader.

现在,这两种方法都有一个明显的缺点,即为了执行转换,整个 XML 需要同时存在于内存中。在 Jackson 支持将树形结构作为标记进行流式处理之前,我们将被这种限制所困,或者我们需要使用类似 XMLStreamReader 的东西来推出我们自己的产品。

7. Conclusion

7.结论

In this tutorial, we briefly learned different ways in which Jackson can read XML data and write it to JSON. Also, we took a quick look at the limitations of each supported approach.

在本教程中,我们简要地了解了Jackson读取XML数据并将其写入JSON的不同方法。此外,我们还快速浏览了每种支持的方法的局限性。

As usual, the full source code that accompanies the tutorial is available over on GitHub.

像往常一样,伴随着该教程的完整源代码可在GitHub上获得