+-
编组Java时JAXB将xmlns添加到元素中
使用JAXB编组 Java对象时,如何在元素中添加xmlns,因此结果应类似于

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

我的Java实体类例如:

@XmlRootElement
public class R {

    private String a;

    public String getA() {
        return a;
    }

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

试图将名称空间添加到XmlElement中,但是结果不是我期望的.请任何帮助表示赞赏.

最佳答案
在您的示例中,类型属性是名称空间限定的.您可以在@XmlAttribute批注上为其设置名称空间参数.

[R

import javax.xml.bind.annotation.*;

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

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

}

一种

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class A {

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

    @XmlValue
    private String value;

}

欲获得更多信息

> http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
> http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

点击查看更多相关文章

转载注明原文:编组Java时JAXB将xmlns添加到元素中 - 乐贴网