728x90
JAXB(Java Architecture for XML Binding)는 자바 클래스를 XML로 표현하는 자바 API이다. JAXB는 주로 2가지 기능이 있다. 자바 객체를 XML로 직렬화하는 것이고 반대로 XML에서 자바 객체로 역직렬화하는 것이다. 즉, JAXB는 메모리의 데이터를 XML 형식으로 변환하여 저장할 수 있고, 이 과정을 위해 프로그램의 각 클래스에서 XML을 읽고 저장하는 일을 구현해야 한다.
※ 사용법
1. XML 최상단 태그(Root Element)엔 @XmlRootElement(name = "Envelope") 사용
2. 각 Element 별 @XmlElement 사용
3. Element 내 xmlns인 url이 있을 경우 namespace로 구분하며, 하위 element 내 모두 namespace 사용
Response.java
@XmlElement(name = "오늘", namespace = "http://www.google.com")
Today.java
@XmlElement(name = "오늘일자", namespace = "http://www.google.com")
사용한 파일 목록
XmlTest.java - 메인 파일
XmlTest.xml - 언마샬링할 xml 파일
JaxbUtils.java - 마샬링/언마샬링 유틸 파일
Envelope.java
─ Header.java
─ Body.java
─ Response.java
─ Today.java
─ Tomorrow.java
XmlTest.xml
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
</Header>
<Body>
<response>
<오늘 xmlns="http://www.google.com">
<오늘일자>
20230101
</오늘일자>
</오늘>
<내일>
<내일일자>
20230910
</내일일자>
</내일>
</response>
</Body>
</Envelope>
Envelope.java
package com.test.xml;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Envelope")
public class Envelope {
private Header Header;
private Body Body;
public Header getHeader() {
return Header;
}
public void setHeader(Header header) {
Header = header;
}
public Body getBody() {
return Body;
}
public void setBody(Body body) {
Body = body;
}
}
Header.java
package com.test.xml;
import lombok.Data;
@Data
public class Header {
}
728x90
Body.java
package com.test.xml;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class Body {
@XmlElement
private Response response;
public Response getResponse() {
return response;
}
public void setResponse(Response response) {
this.response = response;
}
}
Response.java
package com.test.xml;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
@XmlElement(name = "오늘", namespace = "http://www.google.com")
private Today today;
@XmlElement(name = "내일")
private Tomorrow tomorrow;
public Today getToday() {
return today;
}
public void setToday(Today today) {
this.today = today;
}
public Tomorrow getTomorrow() {
return tomorrow;
}
public void setTomorrow(Tomorrow tomorrow) {
this.tomorrow = tomorrow;
}
}
반응형
Today.java
package com.test.xml;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class Today {
@XmlElement(name = "오늘일자", namespace = "http://www.google.com")
private String todayDate;
public String getTodayDate() {
return todayDate;
}
public void setTodayDate(String todayDate) {
this.todayDate = todayDate;
}
}
Tomorrow.java
package com.test.xml;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class Tomorrow {
@XmlElement(name = "내일일자")
private String tomorrowDate;
public String getTomorrowDate() {
return tomorrowDate;
}
public void setTomorrowDate(String tomorrowDate) {
this.tomorrowDate = tomorrowDate;
}
}
JaxbUtils.java
package com.test.util;
import java.io.File;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class JaxbUtils {
/**
* Marshaller
*
* @param object
* @return string
*/
public static String xmlToString(Object object) {
String xml = null;
JAXBContext jaxbContext = null;
try {
jaxbContext = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
StringWriter stringwriter = new StringWriter();
stringwriter.append("<?xml version=\1.0\" encoding=\"UTF-8\"?>");
marshaller.marshal(object, stringwriter);
xml = stringwriter.toString();
} catch (JAXBException e) {
e.printStackTrace();
}
return xml;
}
/**
* Unmarshaller
*
* @param object
* @param file
* @return object
*/
public static Object stringToXml(Object object, File file) {
JAXBContext jaxbContext = null;
try {
jaxbContext = JAXBContext.newInstance(object.getClass());
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
object = (Object) unmarshaller.unmarshal(file);
} catch (JAXBException e) {
e.printStackTrace();
}
return object;
}
/**
* Unmarshaller
*
* @param clazz
* @param strXmlData
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T stringToXml(Class<T> clazz, String strXmlData) {
JAXBContext jaxbContext = null;
T result = null;
try {
jaxbContext = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
result = (T) unmarshaller.unmarshal(new StringReader(strXmlData));
} catch (JAXBException e) {
e.printStackTrace();
}
return result;
}
}
XmlTest.java
package com.test;
import java.io.File;
import com.test.util.JaxbUtils;
import com.test.xml.Envelope;
import com.test.xml.Response;
public class XmlTest {
public static void main(String[] args) {
File file = new File(XmlTest.class.getResource("XmlTest.xml").getFile());
Envelope envelope = new Envelope();
envelope = (Envelope) JaxbUtils.stringToXml(envelope, file);
Response envelopeResponse = envelope.getBody().getResponse();
String todayDate = envelopeResponse.getToday().getTodayDate();
String tomorrowDate = envelopeResponse.getTomorrow().getTomorrowDate();
System.out.println("오늘일자 : " + todayDate);
System.out.println("내일일자 : " + tomorrowDate);
}
}
728x90
반응형
'Java' 카테고리의 다른 글
VS Code 한국어 설정하기 (0) | 2024.02.15 |
---|---|
오라클 테이블/컬럼 정보 조회 (0) | 2023.10.10 |
STS 설치하기 (0) | 2023.03.09 |
599_npm install -g @vue/cli 에러 (0) | 2022.07.01 |
jQuery Timepicker (0) | 2021.11.23 |