SoFunction
Updated on 2025-03-09

Java method to convert xml format parameters to json

1. Define entity class

import ;
import ;
@XmlRootElement(name = "User")
@Setter
@ToString
public class User {
    private String name;
    @XmlElement(name = "username")
    public String getName() {
        return name;
    }
}

Note that you need to use the @XmlElement annotation on the get method. When you actually use it, you will find that if it is directly applied to the field, an error will be reported.

2. Use jaxb to connect to entity class

import ;
import ;
import ;
public User fromXml(String xmlData) throws Exception {
    JAXBContext jaxbContext = ();
    Unmarshaller unmarshaller = ();
    return (User) (new StringReader(xmlData));
}

Here is the xmlData example:<User><username>yogima<username></User>
You can convert it to the corresponding field of the entity class

3. Change to json

import ;
public String toJson(User user) throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    return (user);
}

In fact, when returning to the front end, you don’t need to manually go to writeValueAsString by yourself. Here is just an example.
The field name used after the above conversion to json is still used. If you need to keep the field name "username", you only need to add @JsonProperty.

import ;
import ;
import ;
@XmlRootElement(name = "User")
@Setter
@ToString
public class User {
	@JsonProperty("username")
    private String name;
    @XmlElement(name = "username")
    public String getName() {
        return name;
    }
}

This way, sometimes it is quite common to see annotations. You can use username directly to use username for fields in the User class; but for example, the returned field is<ABC_AA>12345</ABC_AA><AAA>111</AAA>, If I do not specify @JsonProperty and @XmlElement, and directly define the fields as capital ABC_AA and AAA, I can also receive xml parameters, but they do not comply with Java naming specifications, and I will find that the json field transferred by AAA is called aaa, while ABC_AA is transferred by ABC_AA. So it will be more elegant to add comments.

@JsonProperty("ABC_AA")
private String abcAa;
@XmlElement(name = "ABC_AA")
public String getAbcAa() {
    return abcAa;
}

4. The returned xml is more complicated and needs to be extracted

import ;
import ;
public class XmlConvertTest {
    public static void main(String[] args) throws Exception{
    //Simulate more complex xml data    String xmlData = "&lt;response&gt;&lt;head&gt;&lt;response_id&gt;AA&lt;/response_id&gt;&lt;status_code&gt;200&lt;/status_code&gt;&lt;/head&gt;&lt;result&gt;&lt;![CDATA[&lt;row&gt;&lt;AAA&gt;111&lt;/AAA&gt;&lt;BBB&gt;222&lt;/BBB&gt;&lt;/row&gt;&lt;row&gt;&lt;AAA&gt;333&lt;/AAA&gt;&lt;BBB&gt;444&lt;/BBB&gt;&lt;/row&gt;]]&gt;&lt;/result&gt;&lt;/response&gt;";
    //Use regularity to retrieve the required data content, that is, <row><AAA>111</AAA><BBB>222</BBB></row><row><AAA>333</AAA><BBB>444</BBB></row>    String cdataContent = ("(?s).*?&lt;result&gt;&lt;!\\[CDATA\\[(.*?)]]&gt;&lt;/result&gt;.*", "$1");
    // If you need to extract the head part, that is, <response_id>AA</response_id><status_code>200</status_code>    // String headContent = ("(?s).*?&lt;head&gt;(.*?)&lt;/head&gt;.*", "$1");
    //Wrapped with rows    String wrappedContent = "&lt;rows&gt;" + cdataContent + "&lt;/rows&gt;";
    JAXBContext jaxbContext = ();
    Unmarshaller unmarshaller = ();
    XmlTestDTO dto = (XmlTestDTO) (new StringReader(wrappedContent));
    (dto);
}

Corresponding XmlTestDTO:

@XmlRootElement(name = "rows")
@Setter
@ToString
public class XmlTestDTO {
	private List&lt;XmlRowTest&gt; rows = new ArrayList&lt;&gt;();
    @XmlElement(name = "row") // Corresponding to each <row> element    public List&lt;XmlRowTest&gt; getRows() {
        return rows;
    }
    // If XmlRowTest does not need to be used as the root element, you can do not use @XmlRootElement annotation here.    //@XmlRootElement(name = "row")
	@Setter
	@ToString
    public static class XmlRowTest{
        @JsonProperty("AAA")
        private String aaa;
        @XmlElement(name = "AAA")
        public String getAaa() {
            return aaa;
        }
    }
}

Get itXmlTestDTO dtoJust use it afterwards()Just be rightList<XmlRowTest>Iterated

This is the article about Java receiving XML format parameters to convert JSON. For more related content to convert Java xml to JSON, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!