jaxb sample code

java 2014.03.17 17:36

    private String convertObjectToXmlStr(Object object) throws Exception {

        StringWriter stringWriter = new StringWriter();

        JAXBContext jaxbContextRequest;

        try {

            jaxbContextRequest = JAXBContext.newInstance(object.getClass());

            Marshaller jaxbMarshaller = jaxbContextRequest.createMarshaller();

            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            jaxbMarshaller.marshal(object, stringWriter);

            String xml = stringWriter.toString();

            return xml;

        } catch (JAXBException e) {

            logger.error("convertObjectToXmlStr faild. cause : " + e.getMessage());


        }

    }

블로그 이미지

기계새

Java, Spring, iBatis, Maven, eclipse 개발관련 잡다한 메모공간입니다.