bruno
bruno

Reputation: 2169

Java to XML using JAXB

I´m using JAXB to marshall an Java object to XML.

I want to make a web service where i put this code and return the xml, but the last line where i do the marshall doesn´t return a string because it´s a handler.

    JAXBContext context = JAXBContext.newInstance(Person.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Person person = new Person("Anonymous", 32, "employee");    

    marshaller.marshal(person, System.out);

Has anyone done this before?

Upvotes: 1

Views: 3925

Answers (3)

Kal
Kal

Reputation: 24910

The marshaller.marshal() has several overriden methods that allow you to marshal an object into an outputstream, File, w3c.dom.Node ... etc.

The better question might be what are you using to write the web service. If you are using JAX-WS, these things are done automatically for you.

http://jaxb.java.net/nonav/2.2.3/docs/api/javax/xml/bind/Marshaller.html

Upvotes: 1

bdoughan
bdoughan

Reputation: 148977

You could marshal to a StringWriter instead.

Upvotes: 1

Related Questions