반응형
오래전부터 XML을 변환하는 데 있어서 XStream을 많이 사용했을 겁니다. 이것을 사용하면 아주 편하게 XML을 제어롤 하게 됩니다.(object를 xml로 or xml을 object로) 그래서 SOAP 인터페이스나 다양한 OPEN API나 멀티미디어 디바이스와의 연동 등에 많이 사용하곤 합니다.
아래의 샘플을 올려놓을테니 한번 실행해 보시고 사용법을 익힌다음 자기의 것으로 만들어 개발에 생산성을 높이는데 일조를 하시면 되겠죠. ^^
1. IXmlRule.java
package com.mimul.xstream;
public interface IXmlRule
{
public String toXML();
}
2. BaseRule.java
package com.mimul.xstream;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.Annotations;
import java.io.Serializable;
import java.lang.reflect.Field;
public class BaseRule implements Serializable {
/**
* XML 문자열로 변환
*/
protected String toXML() {
XStream xstream = new XStream();
Annotations.configureAliases(xstream, getClass());
Field[] fields = getClass().getDeclaredFields();
for (Field f : fields) {
Annotations.configureAliases(xstream, f.getType());
}
String xmlString = xstream.toXML(this);
xstream = null;
return xmlString;
}
/**
* XML 데이터로부터 Rule Build
*/
public static IXmlRule fromXML(String xmlString, Class clazz) {
XStream xstream = new XStream();
Annotations.configureAliases(xstream, clazz);
Field[] fields = clazz.getDeclaredFields();
for (Field f : fields) {
Annotations.configureAliases(xstream, f.getType());
}
IXmlRule rule = (IXmlRule) xstream.fromXML(xmlString);
xstream = null;
return rule;
}
}
3. Member.java
package com.mimul.xstream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("xstream.Member")
public class Member {
private String accountNo;
private String password;
public String getAccountNo() {
return accountNo;
}
public void setAccountNo(String accountNo) {
this.accountNo = accountNo;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4. PersonRule.java
package com.mimul.xstream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import java.util.ArrayList;
import java.util.List;
@XStreamAlias("Person")
public class PersonRule extends BaseRule implements IXmlRule
{
private static final long serialVersionUID = 1L;
public String toXML() {
return super.toXML();
}
private String name;
private int age;
private String occupation;
@XStreamImplicit(itemFieldName = "members")
private List<Member> members;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public List<Member> getMembers() {
return members;
}
public void setMembers(List<Member> members) {
this.members = members;
}
public static void main(String args[])
{
PersonRule person = null;
List<Member> members = null;
Member member = null;
try {
person = new PersonRule();
person.setName("미물");
person.setAge(32);
person.setOccupation("Consultant");
members = new ArrayList<Member>(3);
member = new Member();
member.setAccountNo("mimul");
member.setPassword("mimuladmim");
members.add(member);
member = new Member();
member.setAccountNo("pepsi");
member.setPassword("pepsiadmin");
members.add(member);
member = new Member();
member.setAccountNo("scott");
member.setPassword("tigerda");
members.add(member);
person.setMembers(members);
// Object에서 XML 생성
String xmlString = person.toXML();
System.out.println("XML : " + xmlString);
// XML파일 Object 매핑
PersonRule personRule = (PersonRule) BaseRule.fromXML(xmlString, PersonRule.class);
System.out.println(personRule.getName());
System.out.println(personRule.getAge());
System.out.println(personRule.getOccupation());
for (Member mem : personRule.getMembers()) {
System.out.println(mem.getAccountNo());
System.out.println(mem.getPassword());
}
} catch (Exception e) {
System.out.println(e);
person = null;
members = null;
}
}
}
5. 실행 화면
6. JSON 변환 방법을 알려면 여기를 가시면 됩니다.
출처 : http://mimul.com/pebble/default/2007/11/08/1194530400000.html