1. XML 데이터
2. Bean 설정
3. Repository 소스
4. 모델 클래스
5. 결과 출력
<PARENT>
<HEAD>
<RESULT_CODE>00001</RESULT_CODE>
<RESULT_MESSAGE>success</RESULT_MESSAGE>
</HEAD>
<BODY>
<STATUS>true</STATUS>
</BODY>
</PARENT>
<HEAD>
<RESULT_CODE>00001</RESULT_CODE>
<RESULT_MESSAGE>success</RESULT_MESSAGE>
</HEAD>
<BODY>
<STATUS>true</STATUS>
</BODY>
</PARENT>
2. Bean 설정
<bean id="unmarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<props>
<prop key="PARENT">kr.or.openframework.model.Parent</prop>
</props>
</property>
<property name="autodetectAnnotations" value="true" />
</bean>
<property name="aliases">
<props>
<prop key="PARENT">kr.or.openframework.model.Parent</prop>
</props>
</property>
<property name="autodetectAnnotations" value="true" />
</bean>
3. Repository 소스
@Repository
public class ParentRepository {
@Resource(name = "unmarshaller")
private Unmarshaller unmarshaller;
public Object xmlToObject(URL url) {
InputStream urlInputStream = null;
try {
urlInputStream = url.openConnection().getInputStream();
return unmarshaller.unmarshal(new StreamSource(urlInputStream));
} catch (IOException e) {
} finally {
if (urlInputStream != null) {
try {
urlInputStream.close();
} catch (IOException e) {
}
}
}
}
}
public class ParentRepository {
@Resource(name = "unmarshaller")
private Unmarshaller unmarshaller;
public Object xmlToObject(URL url) {
InputStream urlInputStream = null;
try {
urlInputStream = url.openConnection().getInputStream();
return unmarshaller.unmarshal(new StreamSource(urlInputStream));
} catch (IOException e) {
} finally {
if (urlInputStream != null) {
try {
urlInputStream.close();
} catch (IOException e) {
}
}
}
}
}
4. 모델 클래스
package kr.or.openframework.model;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("PARENT")
public class Parent {
@XStreamAlias("HEAD")
private Head head;
@XStreamAlias("BODY")
private Body body;
public Head getHead() {
return head;
}
public void setHead(Head head) {
this.head = head;
}
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
}
@XStreamAlias("HEAD")
class Head extends BaseModel {
@XStreamAlias("RESULT_CODE")
private String resultCode;
@XStreamAlias("RESULT_MESSAGE")
private String resultMessage;
public String getResultCode() {
return resultCode;
}
public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}
public String getResultMessage() {
return resultMessage;
}
public void setResultMessage(String resultMessage) {
this.resultMessage = resultMessage;
}
}
@XStreamAlias("BODY")
class Body {
@XStreamAlias("STATUS")
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status= status;
}
}
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("PARENT")
public class Parent {
@XStreamAlias("HEAD")
private Head head;
@XStreamAlias("BODY")
private Body body;
public Head getHead() {
return head;
}
public void setHead(Head head) {
this.head = head;
}
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
}
@XStreamAlias("HEAD")
class Head extends BaseModel {
@XStreamAlias("RESULT_CODE")
private String resultCode;
@XStreamAlias("RESULT_MESSAGE")
private String resultMessage;
public String getResultCode() {
return resultCode;
}
public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}
public String getResultMessage() {
return resultMessage;
}
public void setResultMessage(String resultMessage) {
this.resultMessage = resultMessage;
}
}
@XStreamAlias("BODY")
class Body {
@XStreamAlias("STATUS")
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status= status;
}
}
5. 결과 출력
kr.or.openframework.model.Parent@e72f0c[
head=kr.or.openframework.model.Head@12eabae[
resultCode=00001
resultMessage=success
]
body=kr.or.openframework.model.Body@10948cf[
status=true
]
]
head=kr.or.openframework.model.Head@12eabae[
resultCode=00001
resultMessage=success
]
body=kr.or.openframework.model.Body@10948cf[
status=true
]
]
'자바' 카테고리의 다른 글
[MyBatis] MyBatis 3.0 사용자 가이드 한국어 문서 공개 (10) | 2011.08.11 |
---|---|
[SiteMesh] error-page 와 SiteMesh (0) | 2011.08.03 |
[maven] javadoc 생성 (0) | 2011.02.15 |
[Spring] mvc:annotation-driven과 AnnotationMethodHandlerAdapter의 CustomArgumentResolvers (0) | 2011.02.07 |
[Spring] Spring AOP를 통한 선언적인 트랜잭션 관리가 제대로 작동하지 않는 경우 (1) | 2011.02.01 |