Jaxb :Need Namespace Prefix to All the Elements

JAXB :Need Namespace Prefix to all the elements

Solved by adding

@XmlSchema(
namespace = "http://www.example.com/a",
elementFormDefault = XmlNsForm.QUALIFIED,
xmlns = {
@XmlNs(prefix="ns1", namespaceURI="http://www.example.com/a")
}
)

package authenticator.beans.login;
import javax.xml.bind.annotation.*;

in package-info.java

Took help of jaxb-namespaces-missing : Answer provided by Blaise Doughan

Specifying prefix to namespace JAXB

As remarked in the comments, your @XmlSchema lacks a namespace attribute, therefore all @XmlRootElement and @XmlType are considered to be without a namespace, unless explicitly qualified. Your java-package.info should look like this:

@XmlSchema(elementFormDefault = XmlNsForm.QUALIFIED, namespace = "http://foo/3.0/api", xmlns = {
@XmlNs(namespaceURI = "http://foo/3.0/api", prefix = ""),
@XmlNs(namespaceURI = "http://foo/3.0/base", prefix = "base"),
@XmlNs(namespaceURI = "http://foo/1.0/common", prefix = "common")})
package exp._3_0.api;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

The @XmlNs annotation does not specify the default namespace used in the Java package. They only provide a suggestion to the JAXB implementation on what prefixes to assign to those namespaces. In your example the tags <TokenRequest>, <software>, <softwareId> and <softwareName> do not belong to any namespace, therefore JAXB uses an empty prefix for these elements and must use another prefix (ns4) for the elements in the http://foo/3.0/api namespace (there aren't any).

Is it possible to customize the namespace prefix that JAXB uses when marshalling to a String?

http://hwellmann.blogspot.com/2011/03/jaxb-marshalling-with-custom-namespace.html

This shows how to do it.

Another:
http://www.systemmobile.com/?p=280

Key bits in case that link dies too:

the NamespacePrefixMapper class, found in the com.sun.xml.bind.marshaller package. The abstract class has one method to implement:

public abstract String getPreferredPrefix(  
String namespaceUri,
String suggestion,
boolean requirePrefix);

then

Marshaller marshaller =        
jaxbContext.createMarshaller();
marshaller.setProperty(”com.sun.xml.bind.namespacePrefixMapper”,
new MyNamespacePrefixMapper());

If you’re also using javax.xml.xpath.XPath, your NamespacePrefixMapper can also implement javax.xml.namespace.NamespaceContext, centralizing your namespace customization in a single class.

JAXB marshal namespace prefix

Fixed by replacing

@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;

With the expected entity

@XmlElement(namespace="http://www.portalfiscal.inf.br/nfe")
protected TRetEnviNfe;

jaxb namespace gives unneeded ns2 prefix

The namespace prefix is needed, since you have elements like <subtag> without a namespace. Once you assign a namespace to them, the ns2 prefix will disappear and JAXB will use a default namespace as your required.

There is also a comment in NamespaceContextImpl#declareNsUri of JAXB RI that states:

// the default prefix is already taken.
// move that URI to another prefix, then assign "" to the default prefix.

Edit: If you cannot get rid of the unqualified elements, you can qualify them with the same namespace as the other elements. This is possible at least with JAXB RI and is equivalent to a <xsd:include> tag:

final Map<String, String> props = Collections.singletonMap(JAXBRIContext.DEFAULT_NAMESPACE_REMAP, "http://namespace");
final JAXBContext ctx = JAXBContext.newInstance(new Class[]{Create.class}, props);


Related Topics



Leave a reply



Submit