Madi Sagimbekov
Madi Sagimbekov

Reputation: 319

JAXB add xmlns into element when marshalling java

How to add xmlns into Element when marshalling Java Object using JAXB, so the result should be like

<R>  
    <A xmlns:p2="test" p2:type="type">
        some value
    </A>
</R>

my Java entity class is for example:

@XmlRootElement
public class R {

    private String a;

    public String getA() {
        return a;
    }

    @XmlElement(name="A")
    public void setA(String a) {
        this.a = a;
    }
}

Tried to add namespace into XmlElement, but the result is not what I expected. Please, any help is appreciated.

Upvotes: 3

Views: 10425

Answers (1)

bdoughan
bdoughan

Reputation: 149047

In your example it is the type attribute that is namespace qualified. You would set the namespace parameter on the @XmlAttribute annotation for it.

R

import javax.xml.bind.annotation.*;

@XmlRootElement(name="R")
@XmlAccessorType(XmlAccessType.FIELD)
public class R {

    @XmlElement(name="A")
    private A a;

}

A

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class A {

    @XmlAttribute(namespace="test")
    private String type;

    @XmlValue
    private String value;

}

For More Information

Upvotes: 3

Related Questions