Sunny Gupta
Sunny Gupta

Reputation: 7067

Header Tag in XML using JAXB

Right now I am getting this as an XML output from my JAXB Marshaller

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><create></create>

But I want my root element as:

<create xmlns="http://ws.abc.com" xmlns:doc="http://ws.abc.com">

Do I need to modify this using parsers, Or is there any annotation available.

Upvotes: 19

Views: 22594

Answers (2)

bdoughan
bdoughan

Reputation: 149047

You can set the following property on the Marshaller to remove the header:

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

For More Information

Upvotes: 36

speedRS
speedRS

Reputation: 1240

I've used a Transformer in the past. You'd want something like the following sample code:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StreamResult transformedDoc = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(content); // Where content is a org.w3c.dom.Document object.

transformer.transform(source, transformedDoc);

So maybe do your marshalling and then process. Not sure if this is the best approach but it would work.

Upvotes: 1

Related Questions