LinkedNestBlog

[Marshalling] From javaObject To Xml

by 이진원Dev
[Marshalling] From javaObject To Xml


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.] 어노테이션 선언하지 않았을 때, 오류 메시지
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          Exception 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 annotation
              at 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
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
          35
          36
          37
          38
          39
          40
          41
          42
          43
          44
          45
          46
          47
          48
          49
          50
          51
          52
          53
          54
          55
          56
          57
          58
          59
          60
          61
          62
          63
          64
          65
          66
          67
          68
          69
          70
          71
          72
          73
          74
          75
          76
          77
          78
          79
          80
          81
          82
          83
          84
          85
          86
          87
          88
          89
          90
          91
          92
          93
          94
          95
          96
          97
          98
          99
          100
          101
          102
          103
          104
          105
          106
          107
          108
          109
          110
          111
          112
          113
          114
          115
          116
          117
          118
          119
          120
          121
          122
          123
          124
          125
          126
          127
          128
          129
          130
          131
          132
          133
          134
          135
          136
          137
          138
          139
          140
          141
          142
          143
          144
          145
          146
          147
          148
          149
          150
          151
          152
          153
          154
          155
          156
          157
          158
          159
          160
          161
          162
          163
          164
          165
          166
          167
          168
          169
          170
          171
          172
          173
          174
          175
          176
          177
          178
          179
          180
          181
          package 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;
              }
              @Override
              public 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
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          27
          28
          29
          30
          31
          32
          33
          34
          35
          36
          37
          38
          39
          40
          41
          42
          43
          44
          45
          46
          package 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
          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          22
          23
          24
          25
          26
          <?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
[Marshalling] From javaObject To Xml · LinkedNest Blog