관련 사이트 : http://simple.sourceforge.net/home.php
자바를 위한 XML 직렬화처리 및 설정을 해주는 높은 성능의 프레임워크다.
- Simple framework with powerful capabilities
- Can handle cycles in the object graph
- It requires absolutely no configuration
- Extremely rapid development with XML
- Converts to and from human editable XML
- Contains an XML templating system
위의 특징을 가지고 있는 프레임워크다. 자세한 내용은 사이트에 가서 확인하기 바란다.
아래 튜토리얼을 확인하기 바란다.
tutorial : http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php
간단한 예제코드
1. 먼저 Simple framework를 다운로드 받는다.
< Example.java >
package javastudy.simplexml;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root
public class Example {
@Element
private String text;
@Attribute
private int index;
public Example() {
super();
}
public Example(String text, int index) {
super();
this.text = text;
this.index = index;
}
public String getText() {
return text;
}
public int getIndex() {
return index;
}
}
<SimpleXmlTest.java> 테스트 코드
package javastudy.simplexml;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.io.File;
import org.junit.Test;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
/**
* simple Xml serialization 프레임워크를 이용하여 객체를 XML로 변환처리 테스트
* @author 허니몬
*
* 고려사항 :
* 1. Java 객체를 우선 정의해줘야한다.
*/
public class SimpleXmlTest {
/**
* Example 클래스의 구조를 example.xml으로 변환하여 xml파일을 생성한다.
* 이때, 객체 안에 담겨있는 데이터는 @Attribute @Element 애노테이션에
* 의해 xml의 attribute와 element로 정의된다.
*/
@Test
public void simpleObjectToXmlTest() {
Serializer serializer = new Persister();
Example example = new Example("Example message", 123);
File result = new File("example.xml");
try {
serializer.write(example, result);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void simpleXmlToObjectTest() {
Serializer serializer = new Persister();
File source = new File("example.xml");
Example example = null;
try {
example = serializer.read(Example.class, source);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
assertThat(example.getText(), is("Example message"));
assertThat(example.getIndex(), is(123));
}
}
<example.xml> 위의 코드에서 생성된 xml
Example message
의 형태를 띄게 된다. 그 이외의 상세한 내용에 대해서는...
차근차근 해보도록 하자. 요즘 머리에 주입되는 정보들을 처리하느라 힘들다.
사용용도 : 객체(Java source)를 뼈대로 해서 xml파일을 생성하려고 할 때 사용할 수 있을 것이며,
이렇게 생성된 XML에 담긴 정보를 객체에 주입할여 인스턴스를 생성할 수 있을 것이다.