본문 바로가기
Backend/JAVA

[JAVA] 자바 객체를 XML로 변환하기 - JAXB(자바API)를 사용하여 dto를 xml로 변환 (marshaller)

by couque 2023. 6. 23.
반응형

데이터를 주고 받을 때, 주로 json 형태를 사용하지만 가끔 xml을 사용하는 경우도 있어서,

JAXB (자바API)를 이용하여 자바 객체를 xml 형태로 변경해주는 방법에 대해 알아보자.

  • XmlRootElement : XML의 root Element명
  • XmlElement : XML의 각 root 하위 Element 명

# root dto 만들기

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Root")
public class Root_DTO {
    @XmlElement(name="Header")
    private Header_DTO Header;
    
    @XmlElement(name="Body")
    private Body_DTO Body;
    
    public Header_DTO getHeader(){
    	return Header;
    }
    public void setHeader(Header_DTO header){
    	Header = Header;
    }
    
    public Body_DTO getBody(){
    	return Body;
    }
    public void setBody(Body_DTO body){
    	Body = body;
    }
}

 

# header dto 만들기

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Header")
public class Header_DTO {
    @XmlElement(name="ip")
    private String ip;
    
    @XmlElement(name="contentType")
    private String contentType;
    
    public String getIp(){
    	return ip;
    }
    public void setIp(String ip){
    	ip = ip;
    }
    
    public String getContentType(){
    	return contentType;
    }
    public void setContentType(String contentType){
    	contentType = contentType;
    }
}

 

# body dto 만들기

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Body")
public class Body_DTO {
    @XmlElement(name="userId")
    private String userId;
    
    @XmlElement(name="userPw")
    private String userPw;
    
    @XmlElement(name="userName")
    private String userName;
    
    public String getUserId(){
    	return userId;
    }
    public void setUserId(String userId){
    	userId = userId;
    }
    
    public String getUserPw(){
    	return userPw;
    }
    public void setUserPw(String userPw){
    	userPw = userPw;
    }
    
    public String getUserName(){
    	return userName;
    }
    public void setUserName(String userName){
    	userName = userName;
    }
}

 

# dto 데이터 입력 및 xml로 변환

Root_Dto rootDto = new Root_Dto();
Header_Dto headerDto = new Header_Dto();
Body_Dto bodyDto = new Body_Dto();

//header 설정
headerDto.setIp("172.0.0.1");
headerDto.setContentType("application/json");

//body 설정
bodyDto.setUserId("testId");
bodyDto.setUserPw("testPw");
bodyDto.setUserName("테스트");

//root 설정
rootDto.setHeader(headerDto);
rootDto.setBody(bodyDto);

//xml로 변환
JAXBContext context = JAXBContext.newInstance(Root_Dto.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");

StringWriter sw = new StringWriter();
marshaller.marshal(rootDto, sw);

System.out.println("sw : " + sw.toString());

 

반응형

댓글