Get the App
SLTechnology News&Howtos  ›  Development  › 

How XML and JSON convert each other

Shulou Source: shulou.com Published: 2022-06-02 23:57:27 09月30日 Update

This article mainly explains "how XML and JSON are transformed into each other". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how XML and JSON convert each other".

Dependence

In the Web project of Spring Boot or Spring Cloud, you only need to introduce:

Com.fasterxml.jackson.dataformat jackson-dataformat-xml

This is jackson's XML formatting module, which has been automatically added to the dependency pool.

XML and Java objects convert each other

The process of converting XML and JSON to each other is to convert to a Java object and then to the target type.

XmlMapper

XML object conversion needs to define a XmlMapper, which corresponds to the ObjectMapper of the transformation JSON, and even XmlMapper is a subclass of ObjectMapper. Let's declare a XmlMapper, and you can new one if you use the default configuration.

XmlMapper xmlMapper = new XmlMapper ()

If you need to customize the way you recommend using the builder, such as the following:

XmlMapper build = XmlMapper.builder () / / ignore entity classes have no corresponding attributes. Configure (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) will be thrown if it is true False) / / ignore null .serializationInclusion (JsonInclude.Include.NON_NULL) / / attributes use lowercase hump initials. PropertyNamingStrategy (PropertyNamingStrategy.LOWER_CAMEL_CASE) .build ()

Demo

Next, let's demonstrate with a DEMO, first define a XML file for test.xml, which reads

Felord.cn 18]] >

It is much more fragrant and readable than escape characters.

The corresponding entity classes are:

@ Data public class User {private String username; private Integer age; / / private String created;}

Then use the builder constructed above to perform the conversion operation:

ClassPathResource classPathResource = new ClassPathResource (". / test.xml"); User user = build.readValue (classPathResource.getFile (), User.class); / / Java User (username=felord.cn, age=18, created=) System.out.println ("xml file to Java Object" + user); String s = build.writeValueAsString (user); / / felord.cn18 System.out.println ("Java Object to xml string" + s); User u = build.readValue (s, User.class) / / Java User (username=felord.cn, age=18, created=) System.out.println ("xml string to Java Object" + u)

Now that you can transfer objects, it is not difficult to transfer JSON again.

Of course, it also supports the way of annotations.

At this point, I believe you have a deeper understanding of "how XML and JSON convert each other". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Objects content entities attributes methods learning presentation practical deeper next interest readability subclasses letters characters practicality practical lowercase easy to operate files Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Shulou Information Xiaomi Shulou Tech Info