JAXB marshaller - Don't add namespace to every element



I'm trying to wrap my head around JAXB and namespaces. I have a few annotated classes like for example.



@XmlRootElement
public class Message extends AbstractStanza {
@XmlElement
private String body;

public String getBody() {
return this.body;
}
}


and



@XmlRootElement
public class Error {

@XmlElement(namespace = "urn:ietf:params:xml:ns:xmpp-streams")
private String text;

@XmlAnyElement(lax = true)
private List<Object> nodes;

}


All these objects (among some others that look similar) are known in the JAXBContext umarshalling this works fine and as expected. But when I try to marshall a class - the message class for example - I get the xmpp-streams namespace added to all my elements. like this:



<message xmlns:ns2="urn:ietf:params:xml:ns:xmpp-streams"/>


instead I want this to read just <message /> and have that particualar namespace only added when actually marshaling an <text> element as a child of Error.


What about namespaces in JAXB/xml am I missing here? What can I do to fix this?


No comments:

Post a Comment