2

Currently i am mapping a namespace by creating a package-info.java file for a package with the following annotation.

@XmlSchema(elementFormDefault = XmlNsForm.QUALIFIED,
namespace = "http://example.com",
xmlns = {
    @XmlNs(prefix = "i",
    namespaceURI = "http://www.w3.org/2001/XMLSchema-instance")
})

As you can see one of my namespaces has no prefix whilst the other does, this currently works but i want another way of mapping the namespace without having to create a separate file, anybody have an idea of how i could place the namespace mapping inside my class alongside the annotations?

In my XML the namespaces declarations are inside my root element like below:

<RootElement xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://example.com">...

My class declarations and annotations is like below for the root element.

@XmlRootElement(name="RootElement)
public static class RootElement{
   ........
} 

Thanks.

1 Answer 1

4

I hope the following helps:

With @XmlSchema

package-info

@XmlSchema(
        elementFormDefault = XmlNsForm.QUALIFIED,
        namespace = "http://example.com")
package forum20127191;

import javax.xml.bind.annotation.*;

RootElement

package forum20127191;

import javax.xml.bind.annotation.*;

@XmlRootElement(name="RootElement")
public class RootElement {

    private String foo;

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

}

Without @XmlSchema

If you do not wish to specify the namespace qualification on the package level @XmlSchema annotation then you will need to namespace qualify each mapping to an element. Your RootElement class would need to change to look like.

package forum20127191;

import javax.xml.bind.annotation.*;

@XmlRootElement(name="RootElement", namespace="http://example.com")
public class RootElement {

    private String foo;

    @XmlElement(namespace="http://example.com")
    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

}

For More Information

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.