2

I'm trying to marshal objects into XML and I have a problem with the namespaces prefix (I don't want to have any prefix).

I'm using bindigs file to customize the generated classes. The generated package-info.java file contains the following:

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

So, the prefix is set to "", but when the XML is generated, I have a generated prefix. If I use another value for prefix (different than an empty string), the XML is generated with the right prefix.

2 Answers 2

2

With the latest JAXB, you must set QUALIFIED to elementFormDefault field of XmlSchema annotation. Like this:

@XmlSchema (
    elementFormDefault= XmlNsForm.QUALIFIED,
    ...)

Otherwise, JAXB will add an auto prefix (ns1, ns2, ...)

0

Your JAXB (JSR-222) implementation does not guarantee that it will use the prefixes specified in the @XmlSchema annotation. EclipseLink JAXB (MOXy) will and so will recent versions of the JAXB RI.

The JAXB RI offers a NamespacePrefixMapper extension which is now supported by MOXy to give more control over the prefixes used.

For More Information

2
  • I've read about this option before but I've hoped it can be solved only with bindings.
    – ovi2ut
    Oct 9, 2012 at 13:43
  • @ovi2ut - Like I said EclipseLink MOXy and recent versions of the JAXB RI will use the prefixes specified in the @XmlSchema annotation.
    – bdoughan
    Oct 9, 2012 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.