Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove namespace prefix while JAXB marshalling

I have JAXB objects created from a schema. While marshalling, the xml elements are getting annotated with ns2. I have tried all the options that exist over the net for this problem, but none of them works. I cannot modify my schema or change package-info.java. Please help

like image 930
user2487308 Avatar asked Jun 20 '13 20:06

user2487308


People also ask

How do I remove namespace prefix JAXB?

You can use the NamespacePrefixMapper extension to control the namespace prefixes for your use case. The same extension is supported by both the JAXB reference implementation and EclipseLink JAXB (MOXy).

How do I become a marshal without a namespace?

setDefaultNamespace("") solved the issue. One more thing you have to care in order to remove the namespace prefix from the output is that everywhere you have @XmlElement ensure it does not include the namespace property like @XmlElement(name="", namespace"http://...") ; otherwise, none of solutions will work.

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.


2 Answers

After much research and tinkering I have finally managed to achieve a solution to this problem. Please accept my apologies for not posting links to the original references - there are many and I wasn't taking notes - but this one was certainly useful.

My solution uses a filtering XMLStreamWriter which applies an empty namespace context.

public class NoNamesWriter extends DelegatingXMLStreamWriter {    private static final NamespaceContext emptyNamespaceContext = new NamespaceContext() {      @Override     public String getNamespaceURI(String prefix) {       return "";     }      @Override     public String getPrefix(String namespaceURI) {       return "";     }      @Override     public Iterator getPrefixes(String namespaceURI) {       return null;     }    };    public static XMLStreamWriter filter(Writer writer) throws XMLStreamException {     return new NoNamesWriter(XMLOutputFactory.newInstance().createXMLStreamWriter(writer));   }    public NoNamesWriter(XMLStreamWriter writer) {     super(writer);   }    @Override   public NamespaceContext getNamespaceContext() {     return emptyNamespaceContext;   }  } 

You can find a DelegatingXMLStreamWriter here.

You can then filter the marshalling xml with:

  // Filter the output to remove namespaces.   m.marshal(it, NoNamesWriter.filter(writer)); 

I am sure there are more efficient mechanisms but I know this one works.

like image 89
OldCurmudgeon Avatar answered Sep 21 '22 13:09

OldCurmudgeon


For me, only changing the package-info.java class worked like a charm, exactly as zatziky stated :

package-info.java

 @javax.xml.bind.annotation.XmlSchema  (namespace = "http://example.com",  xmlns = {@XmlNs(prefix = "", namespaceURI = "http://example.com")},  elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)  package my.package; import javax.xml.bind.annotation.XmlNs; 
like image 27
cata2d Avatar answered Sep 17 '22 13:09

cata2d