티스토리 뷰

728x90
반응형
SMALL

Java JAXB Marshalling / UnMarshalling

1. Marshalling (POJO to xml)

  • Value Object Class

    @Setter  
    @XmlRootElement  
    public class TestValue {  
     @XmlElement  
    public String name;  
     @XmlElement  
    public int age;  
    }
  • Marshalling

    TestValue testValue = new TestValue();  
    testValue.setAge(30);  
    testValue.setName("name");  
    JAXBContext context = JAXBContext.newInstance(TestValue.class);  
    Marshaller marshaller = context.createMarshaller();  
    marshaller.marshal(testValue, new File("test.xml"));
  • 위에서 생성한 test.xml 파일의 내용

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <testValue>
     <name>name</name>
     <age>30</age>
    </testValue>
  • Unmarshalling ( xml to POJO)

    TestValue testValue = new TestValue();  
    testValue.setAge(30);  
    testValue.setName("name");  
    File file = new File("test.xml");  
    JAXBContext context = JAXBContext.newInstance(TestValue.class);  
    Unmarshaller unmarshaller = context.createUnmarshaller();  
    TestValue testObject = (TestValue) unmarshaller.unmarshal(file);
728x90
반응형
LIST
댓글
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
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
글 보관함