~~ Please see ~~
Add the following to pom
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.8</version>
</dependency>
http://fasterxml.github.io/jackson-dataformat-xml/javadoc/2.9/
-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;
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;
}
DtlsInnerDto.java
@SuppressWarnings("serial")
@Data
@Builder
public class DtlsInnerDto implements Serializeble {
@JacksonXmlProperty(localName = "dtl_item")
private String dtlItem;
}
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
String xml = new XmlMapper().writeValueAsString(Bean assembly process());
<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>
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
Bean class bean= new XmlMapper().readValue(xml string,Bean class.class)
--When you want to express a list http://labs.opentone.co.jp/?p=6634
Recommended Posts