Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove xmlns attribute from the root element while marshalling jaxb

This might be a related to JAXB Marshaller - How do I suppress xmlns namespace attributes?

But my problem is a little different. I do the regular java marshalling and my xsd has no namespaces.The generated xml is without namespaces as well, except for the root element.

<?xml version="1.0" encoding="UTF-8"?><rootElement xmlns:ns2="unwanted namespace">

The unwanted namespace is from another schema from the same project and I am not sure why that is being picked up at this stage.

My rootElement.java generated by jaxb2-maven-plugin looks like :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"feed"
})
@XmlRootElement(name = "rootElement", namespace = "")
public class RootElement{
....
}

At this point all I want is to get rid of the xmlns:ns2="unwanted namespace" attribute from the generated xml and I am struggling with it.

I looked at my package-info.java and it looks like:

@javax.xml.bind.annotation.XmlSchema(namespace = "unwanted namespace", elementFormDefault =   javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.mypackage;

I tried adding he -npa but it wont work on jaxb2-maven-plugin for some reason. I tried the NamespaceMapper but that works for changing prefixes. I could not get it to remove the namespace altogether. This is bothering me for a day now.

like image 315
icedek Avatar asked Apr 03 '13 19:04

icedek


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.

What is marshalling and unmarshalling in JAXB?

JAXB definitionsMarshalling is the process of transforming Java objects into XML documents. Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API. It provides API for marshalling, unmarshalling and validating.

What is marshalling in JAXB?

The JAXB Marshaller interface is responsible for governing the process of serializing Java content trees i.e. Java objects to XML data. This marshalling to XML can be done to variety of output targets.


1 Answers

I have similar requirements at the moment. The only solution that worked for me is implementing a wrapper for XMLStreamWriter.

Please, take a look at my answer here. I've also described there other solutions I tried out.

Serialization using code from the link above looks like this:

XMLOutputFactory factory = XMLOutputFactory.newFactory();

StringWriter writer = new StringWriter(XML_BUFFER_INITIAL_SIZE);
XMLStreamWriter xmlWriter = null;

try {
  xmlWriter = factory.createXMLStreamWriter(writer);
  JAXBContext context = JAXBContext.newInstance(MyJAXBGeneratedClass.class);
  Marshaller marshaller = context.createMarshaller();
  marshaller.marshal(reportContainer, new NamespaceStrippingXMLStreamWriter(xmlWriter));
  xmlWriter.flush();
}
finally {
  if (xmlWriter != null)
    xmlWriter.close();
}

return writer.toString();
like image 169
sergio Avatar answered Oct 13 '22 06:10

sergio