如何将jaxb打印到java对象到logger中

时间:2014-10-01 11:10:54

标签: java apache java-ee logging jaxb

您好,通过以下链接,我可以将JAXB转换为java对象,并使用以下语句在控制台中打印。

http://www.mkyong.com/java/jaxb-hello-world-example/

jaxbMarshaller.marshal(customer, System.out);

但我想在记录器中打印输出。

ex)log.info(客户)或log.debug(客户)

我正在使用Apache log4j。有人有任何想法吗?

1 个答案:

答案 0 :(得分:5)

以下可能的方式..

Customer customer = new Customer();
//set customer attributes 
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Marshaller marshaller = jc.createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(customer, stringWriter );
log.info(stringWriter.toString());