SoFunction
Updated on 2025-04-13

How Java accepts XML format parameters and converts them to JSON

In Java applications, it is common to process XML data and convert it to JSON format. Here is a sample code showing how to do this using Java:

Preparation

1. Make sure your project contains the following dependencies:

<dependency>
    <groupId></groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.0</version>
</dependency>
<dependency>
    <groupId></groupId>
    <artifactId>json</artifactId>
    <version>20210307</version>
</dependency>
<dependency>
    <groupId></groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId></groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.1</version>
</dependency>

2. Create a Java class to represent the XML data structure:

import ;
import ;
import ;

@XmlRootElement(name = "data")
public class Data {
    private String name;
    private int age;
    private List<String> hobbies;

    // Getters and setters

    @XmlElement(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
         = name;
    }

    @XmlElement(name = "age")
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
         = age;
    }

    @XmlElement(name = "hobbies")
    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
         = hobbies;
    }
}

Receive XML parameters

1. Use JAXB to parse XML data:

import ;
import ;
import ;

public class XMLParser {
    public static Data parseXML(String xml) throws Exception {
        JAXBContext context = ();
        Unmarshaller unmarshaller = ();
        StringReader reader = new StringReader(xml);
        Data data = (Data) (reader);
        return data;
    }
}

2. Receive XML parameters in your controller:

import ;
import ;
import ;
import ;
import ;

@RestController
public class DataController {
    @PostMapping("/receive-xml")
    public ResponseEntity<String> receiveXML(@RequestBody String xml) {
        try {
            Data data = (xml);
            String json = (data);
            return new ResponseEntity<>(json, );
        } catch (Exception e) {
            return new ResponseEntity<>("Error processing XML", HttpStatus.BAD_REQUEST);
        }
    }
}

Convert XML to JSON

Use Jackson to convert Java objects to JSON strings:

import ;
import ;
import ;

public class JSONConverter {
    public static String convertToJSON(Data data) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        (SerializationFeature.INDENT_OUTPUT);
        return (data);
    }
}

Test code

Assuming you are using a Spring Boot project, you can test it in the following ways:

Launch your Spring Boot application.

Use a tool such as Postman or curl to send a POST request to /receive-xml with the content type application/xml, with the following XML data:

<data>
    <name>John</name>
    <age>30</age>
    <hobbies>
        <hobby>Reading</hobby>
        <hobby>Traveling</hobby>
        <hobby>Cooking</hobby>
    </hobbies>
</data>

You should receive the following JSON response:

{
  "name" : "John",
  "age" : 30,
  "hobbies" : [ "Reading", "Traveling", "Cooking" ]
}

Summarize

With the above code, we show how to receive parameters in XML format in Java and convert them to JSON format. This process includes the following steps:

  • Use JAXB to parse XML data and bind to Java objects.
  • Use Jackson to convert Java objects to JSON strings.
  • Receive XML requests and return JSON responses via the REST API.

The above is the detailed content of how Java receives XML format parameters and converts them to JSON. For more information about Java receiving XML and converting them to JSON, please pay attention to my other related articles!