Home  >  Article  >  Java  >  How to convert JSON array to XML in Java?

How to convert JSON array to XML in Java?

王林
王林Original
2023-09-06 12:18:411419browse

How to convert JSON array to XML in Java?

How to convert JSON array to XML in Java?

JSON (JavaScript Object Notation) and XML (eXtensible Markup Language) are two commonly used data exchange formats. In Java, we often need to convert between JSON and XML. This article will explain how to convert a JSON array to XML.

First, we need to use a Java library to handle the conversion of JSON and XML. In this article, we will use the Jackson library to process JSON and the dom4j library to process XML. You can add the following dependencies in Maven to use them in your project:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.12.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.12.2</version>
</dependency>
<dependency>
    <groupId>org.dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>2.1.3</version>
</dependency>

Next, we will walk through an example to demonstrate how to convert a JSON array to XML. Suppose we have the following JSON array:

[
  {
    "name": "Alice",
    "age": 25
  },
  {
    "name": "Bob",
    "age": 30
  },
  {
    "name": "Charlie",
    "age": 35
  }
]

We first need to create a Java class to represent each object in the JSON:

public class Person {
    private String name;
    private int age;
    
    // getters and setters
}

Then we can use the Jackson library to convert the JSON array into Java objects List of:

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonToXmlConverter {
    public static void main(String[] args) throws Exception {
        String json = "[{"name":"Alice","age":25},{"name":"Bob","age":30},{"name":"Charlie","age":35}]";
        
        ObjectMapper objectMapper = new ObjectMapper();
        List<Person> persons = objectMapper.readValue(json, new TypeReference<List<Person>>() {});
    }
}

Now, we have converted the data in JSON into a list of Java objects. Next, we will convert it to XML using the dom4j library.

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class JsonToXmlConverter {
    public static void main(String[] args) throws Exception {
        // JSON to Java objects
        String json = "[{"name":"Alice","age":25},{"name":"Bob","age":30},{"name":"Charlie","age":35}]";
        ObjectMapper objectMapper = new ObjectMapper();
        List<Person> persons = objectMapper.readValue(json, new TypeReference<List<Person>>() {});

        // Java objects to XML
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("persons");

        for (Person person : persons) {
            Element personElement = root.addElement("person");
            personElement.addElement("name").setText(person.getName());
            personElement.addElement("age").setText(Integer.toString(person.getAge()));
        }

        String xml = document.asXML();
        System.out.println(xml);
    }
}

Run the above code, the following XML will be output:

<persons>
  <person>
    <name>Alice</name>
    <age>25</age>
  </person>
  <person>
    <name>Bob</name>
    <age>30</age>
  </person>
  <person>
    <name>Charlie</name>
    <age>35</age>
  </person>
</persons>

Through the above code example, we can see how to use the Jackson library to convert the JSON array into a list of Java objects, and then use the dom4j library Convert Java objects to XML.

To summarize, this article explains how to convert JSON array to XML in Java. We used the Jackson library to process JSON and the dom4j library to process XML. I hope this article helps you understand the conversion between JSON and XML.

The above is the detailed content of How to convert JSON array to XML in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn