Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print jaxb to java object in to logger

Hi by following the below link i am able to convert JAXB to java object and print it in console using below statement.

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

jaxbMarshaller.marshal(customer, System.out);

But i want to print the output in logger.

ex)log.info(customer) or log.debug(customer)

I am using Apache log4j.Does anyone have any idea??

like image 738
VelNaga Avatar asked Oct 01 '14 11:10

VelNaga


People also ask

How does JAXB marshalling work?

In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.

Is JAXB Unmarshaller thread safe?

A: The JAXB Specification currently does not address the thread safety of any of the runtime classes. In the case of the Oracle JAXB RI, the JAXBContext class is thread safe, but the Marshaller , Unmarshaller , and Validator classes are not thread safe.


1 Answers

Below a possible way..

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());
like image 146
Xstian Avatar answered Sep 19 '22 11:09

Xstian