tags:

views:

113

answers:

1

Hi, I have a class, let's call it User annotated with @XmlRootElement, with some properties (name, surname etc).

I use this class for REST operations, as application/xml.

The client will POST User class so i want to keep the values in the log.

Is there any method in jaxb to prints out this object as xml?

For instance:

log.info("Customers sent: "+user.whichMethod());

should produce this output:

Customer sent: 
<user> <name>cristi</name> <surname>kevin</surname> </user>

Thanks.

+3  A: 

Found:)

public void toXml() {
    try {
        JAXBContext ctx = JAXBContext.newInstance(User.class);
        Marshaller marshaller = ctx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(this, System.out);
    }
    catch (Exception
            e) {

              //catch exception 
    }
}

Call it like:

log.info("Customers sent: "+user.toXml());
Cristian Boariu
**Never** write an empty catch-block, not even in an example! Examples have the nasty habit of turning into production code at some point and we need to maintain that mess, then.
Joachim Sauer