[Marshalling] From javaObject To Xml
[Marshalling] From javaObject To Xml
Ref.] http://lstierneyltd.com/blog/development/examples/java-to-xml-xml-to-java-marshalling-and-unmarshalling/
Ref.] http://lstierneyltd.com/blog/development/examples/java-to-xml-xml-to-java-marshalling-and-unmarshalling/
- Marshalling이란?
- 객체 메모리 구조를 저장하거나 전송을 위해 Xml 등의 적당한 자료의 형태로 변형하는 것.
- 보통 서로 다른 서버 혹은 서로 다른 프로그램 간 데이터의 이동이 발생시 사용.
- 사용 예제
- From Java Object To Xml
- Dto Class :
- class 명 위에 @XmlRootElement Annotation이 선언되어 있어야 한다. 그렇지 않으면 아래와 같이 오류를 발생할 수 있다.
- Ref.] 어노테이션 선언하지 않았을 때, 오류 메시지
1234567891011121314Exception in thread "main" javax.xml.bind.MarshalException- with linked exception:[com.sun.istack.internal.SAXException2: unable to marshal type "com.firstproj.profile.dto.ProfileStatPitcherDto" as an element because it is missing an @XmlRootElement annotation]at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:95)at com.firstproj.common.marshal.ObjectToXML.main(ObjectToXML.java:43)Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "com.firstproj.profile.dto.ProfileStatPitcherDto" as an element because it is missing an @XmlRootElement annotationat com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:237)at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:322)at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483)at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)... 3 more
cs - Source Code
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181package com.firstproj.profile.dto;import javax.xml.bind.annotation.XmlRootElement;import org.apache.ibatis.type.Alias;@XmlRootElement@Alias("profile.profileStatPitcherDto")public class ProfileStatPitcherDto{private int statPitcherId;private int profileId;private String pTeamName;private String pStatYear;private float inn;private int w;private int l;private float era;private int g;private int gs;private int sv;private int svo;private float ip;private int h;private int r;private int er;private int hr;private int bb;private int so;private float avg;private float whip;private int k;private String etc;public int getStatPitcherId() {return statPitcherId;}public void setStatPitcherId(int statPitcherId) {this.statPitcherId = statPitcherId;}public String getpTeamName() {return pTeamName;}public void setpTeamName(String pTeamName) {this.pTeamName = pTeamName;}public String getpStatYear() {return pStatYear;}public void setpStatYear(String pStatYear) {this.pStatYear = pStatYear;}public float getInn() {return inn;}public void setInn(float inn) {this.inn = inn;}public int getW() {return w;}public void setW(int w) {this.w = w;}public int getL() {return l;}public void setL(int l) {this.l = l;}public float getEra() {return era;}public void setEra(float era) {this.era = era;}public int getG() {return g;}public void setG(int g) {this.g = g;}public int getGs() {return gs;}public void setGs(int gs) {this.gs = gs;}public int getSv() {return sv;}public void setSv(int sv) {this.sv = sv;}public int getSvo() {return svo;}public void setSvo(int svo) {this.svo = svo;}public float getIp() {return ip;}public void setIp(float ip) {this.ip = ip;}public int getH() {return h;}public void setH(int h) {this.h = h;}public int getR() {return r;}public void setR(int r) {this.r = r;}public int getEr() {return er;}public void setEr(int er) {this.er = er;}public int getHr() {return hr;}public void setHr(int hr) {this.hr = hr;}public int getBb() {return bb;}public void setBb(int bb) {this.bb = bb;}public int getSo() {return so;}public void setSo(int so) {this.so = so;}public float getAvg() {return avg;}public void setAvg(float avg) {this.avg = avg;}public int getK() {return k;}public void setK(int k) {this.k = k;}public float getWhip() {return whip;}public void setWhip(float whip) {this.whip = whip;}public String getEtc() {return etc;}public void setEtc(String etc) {this.etc = etc;}public int getProfileId() {return profileId;}public void setProfileId(int profileId) {this.profileId = profileId;}@Overridepublic String toString() {return "ProfileStatPitcherDto [statPitcherId=" + statPitcherId + ", profileId=" + profileId + ", pTeamName="+ pTeamName + ", pStatYear=" + pStatYear + ", inn=" + inn + ", w=" + w + ", l=" + l + ", era=" + era+ ", g=" + g + ", gs=" + gs + ", sv=" + sv + ", svo=" + svo + ", ip=" + ip + ", h=" + h + ", r=" + r+ ", er=" + er + ", hr=" + hr + ", bb=" + bb + ", so=" + so + ", avg=" + avg + ", whip=" + whip+ ", k=" + k + ", etc=" + etc + "]";}}
cs
- Test Class
- Source Code
12345678910111213141516171819202122232425262728293031323334353637383940414243444546package com.firstproj.common.marshal;import javax.xml.bind.JAXBContext;import javax.xml.bind.Marshaller;import com.firstproj.profile.dto.ProfileStatPitcherDto;public class ObjectToXML {public static void main(String[] args) throws Exception{JAXBContext context = JAXBContext.newInstance(ProfileStatPitcherDto.class);Marshaller m = context.createMarshaller();m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);ProfileStatPitcherDto obj = new ProfileStatPitcherDto();obj.setAvg(0);obj.setBb(0);obj.setEr(0);obj.setEra((float)1.0);obj.setEtc(null);obj.setG(1);obj.setGs(2);obj.setH(10);obj.setHr(10);obj.setInn((float)10.1);obj.setIp(0);obj.setK(7);obj.setL(0);obj.setProfileId(11);obj.setpStatYear("2016");obj.setpTeamName("pitcherTeam");obj.setR(1);obj.setSo(11);obj.setStatPitcherId(20);obj.setSv(0);obj.setSvo(1);obj.setW(20);obj.setWhip((float)0.012345);m.marshal(obj, System.out);}}
cs - Result
1234567891011121314151617181920212223242526<?xml version="1.0" encoding="UTF-8" standalone="yes"?><profileStatPitcherDto><avg>0.0</avg><bb>0</bb><er>0</er><era>1.0</era><g>1</g><gs>2</gs><h>10</h><hr>10</hr><inn>10.1</inn><ip>0.0</ip><k>7</k><l>0</l><profileId>11</profileId><r>1</r><so>11</so><statPitcherId>20</statPitcherId><sv>0</sv><svo>1</svo><w>20</w><whip>0.012345</whip><pStatYear>2016</pStatYear><pTeamName>pitcherTeam</pTeamName></profileStatPitcherDto>
cs
- Source Code
- Dto Class :
- From Java Object To Xml