Eric B.
Eric B.

Reputation: 24411

JAXB Get namespace from non-root element?

Is there a way using JAXB to get the namespace programatically from a non-root element?

I can use the following on a root element (where Detailedreport is the root element object):

    QName qname = jaxb.getJaxbContext().createJAXBIntrospector().getElementName( new Detailedreport());
    final String namespace = qname.getNamespaceURI();

However, if I try using a type from the same package, but a non-root element, qname is null.

According to the javadocs for JAXBIntrospector:

Parameter object is a JAXB element for following cases:

It is an instance of javax.xml.bind.JAXBElement. The class of object is annotated with @XmlRootElement.

Given that my non-root element obviously doesn't have the @XmlRootElement, is there a way to get the qname/namespace for it?

Alternatively, is there a way to determine programtically which class represents the root element in a package (without having to scan all the classes myself for the annotation)? I see the namespace in the package-info.java file, but not sure how to get at it.

Upvotes: 1

Views: 1298

Answers (1)

Eric B.
Eric B.

Reputation: 24411

I left the question here in case anyone else runs into the same question.

The namespace for the package is in the package-info.java file. This information can be retrieved directly using java methods:

    String namespace = XmlElementName.class.getPackage().getAnnotation(XmlSchema.class).namespace();

Upvotes: 4

Related Questions