프로그래밍 이야기

JAXB를 이용한 XML Marshalling, UnMarshalling(InputStream, OutputStream) 예제 #3

박디 2013. 8. 19. 23:07

1부에서는 파일을 통한 마샬링, 언마샬링 처리, 2부에서는 HttpUrlConnection을 통해 얻어온 InputStream을 언마샬링

 

하는 방법에 대해 알아봤습니다. 이번엔 마샬링한 데이터를 OutputStream에 저장하고 InputStream으로 변환하여

 

언마샬링 처리 하는 예제를 작성해봤습니다. 일반적으로 이렇게 쓰이는 사례는 거의 없을듯 합니다.

 

-XML 원본 데이터 예시(UnMarshalling시 사용)

 <userBltnList>
     <bltnList>
         <isSecret>Y</isSecret>
         <name>TEST</name>
         <no>TESTNO</no>
         <readNum>TESTREADNUM</readNum>
         <subject>Android GCM Push with Command Pattern</subject>
         <writeDay>2013-02-15 18:00:18.0</writeDay>
     </bltnList>
 </userBltnList>

-----------------------------------------------------------------------------------------------------------------

※ 마샬링한 데이터를 OutputStream으로 생성

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

 

JAXBContext jaxbContext = null;
Marshaller jaxbMarshaller = null;

 

//list는 List(Collection 클래스)

UserBltnList userBltnList = new UserBltnList();
userBltnList.setList(list);
  
ByteArrayOutputStream ous = new ByteArrayOutputStream();
ByteArrayInputStream xmlStream = null;
byte[] byteData = null;
   
jaxbContext = JAXBContext.newInstance(UserBltnList.class);
jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); 
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
jaxbMarshaller.marshal(userBltnList, ous);  //마샬링된 데이터를 OutputStream에 저장 
byteData = ous.toByteArray(); //OutputStream을 Byte로 처리
xmlStream = new ByteArrayInputStream(byteData); //Byte를 InputStream으로 처리  

-----------------------------------------------------------------------------------------------------------------

 

- UserBltnList Class

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

 

@XmlRootElement(name="userBltnList")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserBltnList {
 
 //Marshalling시에 Element의 이름을 정해준다. 반대로 UnMarshalling시에는
 //이 이름에 해당하는 Element속성이나 Element의 내용을 가져오게 된다.
 @XmlElement(name="bltnList")
 private List<UserBltnListVO> list = null;

 public List<UserBltnListVO> getList() {
  if(list==null){
   list = new ArrayList<UserBltnListVO>();
  }
  return list;
 } 
 public void setList(List<UserBltnListVO> list) {
  this.list = list;
 }
}

 

-------------------------------------------------------------------------------------------------

- UserBltnListVO(Value Object Class)

public class UserBltnListVO {
 private String no = "";
 private String subject = "";
 private String writeDay = "";
 private String readNum = "";
 private String isSecret = "";
 private String name = "";

 ..

  getter/setter 생략

 ..

}

-----------------------------------------------------------------------------------------------------------------

 

-----------------------------------------------------------------------------------------------------------------

※ OuputStream에 저장된 마샬링된 데이터를 InputStream으로 변환하여 언마샬링 처리

UserBltnListUnMarshall userBltnListUnMarshall = null;

jaxbContext = JAXBContext.newInstance(UserBltnListUnMarshall.class);
userBltnListUnMarshall =

 (UserBltnListUnMarshall) jaxbContext.createUnmarshaller().unmarshal(xmlStream);

-----------------------------------------------------------------------------------------------------------------

 

-----------------------------------------------------------------------------------------------------------------

- UserBltnListUnMarshall Class

@XmlRootElement(name="userBltnList")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserBltnListUnMarshall {
 
 //Marshalling시에 Element의 이름을 정해준다. 반대로 UnMarshalling시에는
 //이 이름에 해당하는 Element속성이나 Element의 내용을 가져오게 된다.
 @XmlElement(name="bltnList")
 private List<UserBltnListVOUnmarshall> bltnList = null;

 public List<UserBltnListVOUnmarshall> getList() {
  return bltnList;
 }
 public void setList(List<UserBltnListVOUnmarshall> bltnList) {
  this.bltnList = bltnList;
 }
}

-----------------------------------------------------------------------------------------------------------------

 

-----------------------------------------------------------------------------------------------------------------

 - UserBltnListVOUnmarshall(Value Object Class)

@XmlAccessorType(XmlAccessType.FIELD)
public class UserBltnListVOUnmarshall {
 
 //UserBltnListUnMarshall에서 루트 Element인 userBltnList를 가져오도록 하고
 //집합 Element인 bltnList를 컬렉션 형태로 가져오도록 했다. 이 bltnList의
 //Element를 XmlElement Annotation을 통해 가져오도록 한다.
 //(Marshalling때와 반대로 UnMarshalling때는 XmlElement나 XmlAttribute Annotation
 //으로 지정한 명을 원본 XML의 Element나 Attribute명을 읽어오게 된다.)
 
 @XmlElement(name="no")
 private String no = "";
 @XmlElement(name="subject")
 private String subject = "";
 @XmlElement(name="writeDay")
 private String writeDay = "";
 @XmlElement(name="readNum")
 private String readNum = "";
 @XmlElement(name="isSecret")
 private String isSecret = "";
 @XmlElement(name="name")
 private String name = "";

 ..

  getter/setter 생략

 ..

}

-----------------------------------------------------------------------------------------------------------------

 

JAXB의 Unmashaller가 OutputStream을 알아서 처리해 줬다면 참 좋았겠지만 지원하지 않으므로.. 위와같은 방법으로 사용할

 

수 있음을 예제로 만들어 봤습니다. 다음 예제에서는 최종으로 HttpServletResponse객체의 getWriter()를 사용하여 마샬링된

 

데이터를 바로 출력할 수 있는 예제를 작성해 보겠습니다.