클래스에 @XmlRootElement annotation을 사용하지 않은 obejct를 마샬링할 때 사용하면 유용할듯.


public String getMarshallingString(Object obj, String uri, String rootElement) throws Exception {
    JAXBContext jaxbContext;
    StringWriter st = new StringWriter();
    try {
        jaxbContext = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(new JAXBElement(
                  new QName("http://map.star", obj.getClass().getSimpleName()), obj.getClass(), obj ), st);

        return XMLPrinter.prettyFormat(st.toString());
    } catch(Exception e) {
        e.printStackTrace();
        throw e;
    }
}

'java > xml' 카테고리의 다른 글

[JAXB] - unmarshalling, marshalling  (0) 2010.06.29
[xml] - xjc.exe 를 사용한 xsd to java class 생성  (0) 2010.06.29
[xml] - xml to xsd  (0) 2010.06.29
Posted by 짱가쟁이