Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JDK's JAXB without ns2 prefix

After having read all posts about this on Oracle forums, Stackoverflow, java.net I'm finally posting here. I'm using JAXB to create XML files but the problem is that it adds the famous ns2 prefix before my elements, I have tried all the solutions no one worked for me. java -version gives "1.6.0_37"

Solution 1 : Using package-info.java

I created the file in my package containing my @Xml* annotated classes with the following content :

@XmlSchema(
    namespace = "http://mynamespace",
    elementFormDefault = XmlNsForm.QUALIFIED,
    xmlns = {
        @XmlNs(namespaceURI = "http://mynamespace", prefix = "")
    }
)
package com.mypackage;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

Solution 2 : NamespacePrefixMapper

I created the following class and set the mapper to the marshaller :

// Change mapper to avoid ns2 prefix on generated XML
class PreferredMapper extends NamespacePrefixMapper {
    @Override
    public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
        return "";
    }
}
NamespacePrefixMapper mapper = new PreferredMapper();
try {
    marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper);
}
catch (PropertyException e) {
   logger.info("No property for com.sun.xml.bind.namespacePrefixMapper found : " + e.getMessage());
}

With com.sun.xml.bind.namespacePrefixMapper nothing happens, with com.sun.xml.internal.bind.namespacePrefixMapper, it throws the exception.

I've also addded the maven dependency in my pom, but it seems that JRE version has a higher priority :

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.4</version>
</dependency>

Can you help me on this ?

PS : I can't include directly the jar in my classpath for build reasons. PS2 : I can't use JDK7. Thanks in advance.

like image 566
c4k Avatar asked Jan 30 '13 10:01

c4k


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 set namespace prefix in JAXB?

If you are using the default JAXB implementation provided with Java 6 or later, you can configure the namespace prefixes by extending the NamespacePrefixMapper and setting a property to tell the marshaller to use your extension.

What is ns2 in XML?

ns2="http://www.two.com" None. desc : An element within the product element in the XML document. "http://www.default.com" The desc element uses the default namespace, which does not contain a prefix.

What is namespace in XML document?

An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.


1 Answers

Without the implementation of MOXy is not possible. JAXB if the preferred prefix is "", it generates a new one.

I had the same problem in the past, and I configured each prefix for each package-info.java.

NamespacePrefixMapper says in JAVADOC

null if there's no prefered prefix for the namespace URI.
In this case, the system will generate a prefix for you.

Otherwise the system will try to use the returned prefix,
but generally there's no guarantee if the prefix will be
actually used or not.

return "" to map this namespace URI to the default namespace.
Again, there's no guarantee that this preference will be
honored.

If this method returns "" when requirePrefix=true, the return
value will be ignored and the system will generate one"

else if use package-info

we know we can't bind to "", but we don't have any possible name at hand.
generate it here to avoid this namespace to be bound to "".

I hope I've given you all the answers about your question.

like image 104
Xstian Avatar answered Feb 27 '23 14:02

Xstian