반응형
ThoughtWorks에서 만든 Xstream을 활용한다.
Xstream 최대의 장점은 초 간단...
얼마나 간단하면 튜토리얼이 Two Minute Tutorial 이다.
클래스 패스에 xstream-xxx.jar를 넣고.. 다음 코드를 실행하면..
XStream xstream = new XStream();
Person joe = new Person();
joe.setAge(30);
joe.setName("Joe");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
joe.setAge(30);
joe.setName("Joe");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
String xml = xstream.toXML(joe);
System.out.println(xml);
출력 내용은.. 간단하기 이를데 없다.. 만족.. ^^
<object.Person>
<name>Joe</name>
<age>30</age>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
</object.Person>
<name>Joe</name>
<age>30</age>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
</object.Person>
object.Person은 좀 보기 싫어.. 별칭(alias)를 준다.
XStream xstream = new XStream();
xstream.alias("person", Person.class);
Person joe = new Person();
joe.setAge(30);
joe.setName("Joe");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
joe.setAge(30);
joe.setName("Joe");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
String xml = xstream.toXML(joe);
System.out.println(xml);
그럼.. 출력은 내 스타일(?)로 변모...
<person>
<name>Joe</name>
<age>30</age>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
</person>
<name>Joe</name>
<age>30</age>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
</person>
null값을 갖는 속성은 어찌 되는가?
XStream xstream = new XStream();
xstream.alias("person", Person.class);
xstream.alias("person", Person.class);
Person joe = new Person();
// joe.setAge(30);
joe.setName("Joe");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
// joe.setAge(30);
joe.setName("Joe");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
출력이 안되는군.
<person>
<name>Joe</name>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
</person>
<name>Joe</name>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
</person>
XML을 자바 객체로..
XStream xstream = new XStream();
xstream.alias("person", Person.class);
xstream.alias("person", Person.class);
String xml =
"<person>" +
"<name>Joe</name>" +
"<fax>" +
"<code>123</code>" +
"<number>9999-999</number>" +
"</fax>" +
"<phone>" +
"<code>123</code>" +
"<number>1234-456</number>" +
"</phone>" +
"</person>" ;
"<person>" +
"<name>Joe</name>" +
"<fax>" +
"<code>123</code>" +
"<number>9999-999</number>" +
"</fax>" +
"<phone>" +
"<code>123</code>" +
"<number>1234-456</number>" +
"</phone>" +
"</person>" ;
Person joe = (Person)xstream.fromXML(xml);
alias 처리를 포함해도 세 문장이면 되는구만.
단.. 자바 객체 변환(Deserializing)시에는 xpp.jar(xstream zip 파일의 lib에 있음)가 필요하다.