I tried to convert JavaBean and XML with Jackson formatter XML in this era

Background

~~ Please see ~~

Preparation

Add the following to pom


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

Detailed usage

http://fasterxml.github.io/jackson-dataformat-xml/javadoc/2.9/

Various class preparation

Premise

-I used Lombok's @Builder when creating bean data (not mentioned in this article)   https://projectlombok.org/

Bean

TestBean.java


@Data
@Builder
@JacksonXmlRootElement(localName = "top_element")
public class TestBean implements Serializable {

    // isAttribute =If true is given, it becomes an attribute
    @JacksonXmlProperty(localName = "id", isAttribute = true)
    private String topElement;

    @JacksonXmlProperty(localName = "test_field")
    private String testField;
    
    //You can also write nested beans
    @JacksonXmlProperty(localName = "test_inner_bean")
    private TestInnerBean testInnerBean;

    //You can have a list of beans in the field
    @JacksonXmlElementWrapper(localName = "dtls")
    @JacksonXmlProperty(localName = "dtl")
    private List<DtlsInnerDto> dtlsInnerDto;

Beans to nest

TestInnerBean.java


@SuppressWarnings("serial")
@Data
@Builder
public class TestInnerBean implements Serializable {

    @JacksonXmlProperty(localName = "inner_field_one")
    private String innerFieldOne;

    @JacksonXmlProperty(localName = "inner_field_two")
    private String innerFieldTwo;
}

List of nested beans

DtlsInnerDto.java


@SuppressWarnings("serial")
@Data
@Builder
public class DtlsInnerDto implements Serializeble {
    @JacksonXmlProperty(localName = "dtl_item")
    private String dtlItem;
}

Bean → XML conversion processing code

import com.fasterxml.jackson.dataformat.xml.XmlMapper;

String xml = new XmlMapper().writeValueAsString(Bean assembly process());

Conversion result

<top_element id="xxx">
  <test_field>xxx</test_field>
  <test_inner_bean>
     <inner_field_one>xxx</inner_field_one>
     <inner_field_two>xxx</inner_field_two>
  </test_inner_bean>
 <dtls>
    <dtl>xxx</dtl>
    <dtl>yyy</dtl>
    <dtl>zzz</dtl>
  </dtls>
</top_element>

XML → Bean conversion process code

import com.fasterxml.jackson.dataformat.xml.XmlMapper;

Bean class bean= new XmlMapper().readValue(xml string,Bean class.class)

Note

--When you want to express a list http://labs.opentone.co.jp/?p=6634

Recommended Posts

I tried to convert JavaBean and XML with Jackson formatter XML in this era
I tried to read and output CSV with Outsystems
I tried to convert a string to a LocalDate type in Java
I tried to interact with Java
I tried to get started with WebAssembly
I tried the new era in Java
I tried to build a Firebase application development environment with Docker in 2020
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to implement ModanShogi with Kinx
I tried to measure and compare the speed of GraalVM with JMH
Convert JSON to TSV and TSV to JSON with Ruby
I tried to automatically generate a class to convert from a data class to a Bundle with APT
I tried to verify AdoptOpenJDK 11 (11.0.2) with Docker image
I tried to make Basic authentication with Java
I tried to implement polymorphic related in Nogizaka.
I tried to manage login information with JMX
I tried to organize the session in Rails
I tried to link grafana and postgres [docker-compose]
I also tried WebAssembly with Nim and C
I tried to implement deep learning in Java
I tried to output multiplication table in Java
I tried to link JavaFX and Spring Framework.
I tried to develop an application in 2 languages
I tried to break a block with java (1)
I tried to express the phone number (landline / mobile phone) with a regular expression in Rails and write validation and test
I tried to create a shopping site administrator function / screen with Java and Spring
I tried what I wanted to try with Stream softly.
Convert JSON and YAML in Java (using Jackson and SnakeYAML)
I tried Mastodon's Toot and Streaming API in Java
Convert Java enum enums and JSON to and from Jackson
I tried to implement file upload with Spring MVC
I tried to organize the cases used in programming
I tried to implement TCP / IP + BIO with JAVA
I tried to implement Firebase push notification in Java
[Java 11] I tried to execute Java without compiling with javac
# 2 [Note] I tried to calculate multiplication tables in Java.
I tried to integrate AWS I oT button and Slack
I tried to get started with Spring Data JPA
I tried to create a Clova skill in Java
I tried to make a login function in Java
I tried to summarize various link_to used this time
I tried to implement Stalin sort with Java Collector
How to convert A to a and a to A using AND and OR in Java
I tried to implement the Euclidean algorithm in Java
~ I tried to learn functional programming in Java now ~
I tried to chew C # (reading and writing files)
roman numerals (I tried to simplify it with hash)
I tried to find out what changed in Java 9
Easy to make LINE BOT with Java Servlet Part 2: I tried image messages and templates
I tried to make an application in 3 months from inexperienced
I tried to make an introduction to PHP + MySQL with Docker
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
I tried to increase the processing speed with spiritual engineering
I tried to collect and solve Ruby's "class" related problems.
I tried to summarize the basics of kotlin and java
[Rails] I tried to create a mini app with FullCalendar
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
[jackson] I want to receive JSON values "0" and "1" as boolean
I tried to make Java Optional and guard clause coexist