EclipseLink JAXB (MOXy)根据属性形式限定以不同方式处理元素的前缀限定(如下所示)。
命名空间限定没有错,但我同意尽可能使用默认命名空间更好。您可以使用以下错误跟踪此问题的进度:
一种
package forum6808921;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class A {
private String b;
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
}
演示
package forum6808921;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(A.class);
A a = new A();
a.setB("Hello World");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(a, System.out);
}
}
未设置 attributeFormDefault 的包信息
@XmlSchema(
namespace = "sample.com/x"
, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
)
package forum6808921;
import javax.xml.bind.annotation.*;
输出:
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns="sample.com/x">
<b>Hello World</b>
</a>
设置了 attributeFormDefault 的包信息
@XmlSchema(
namespace = "sample.com/x"
, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
, attributeFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
)
package forum6808921;
import javax.xml.bind.annotation.*;
输出:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:a xmlns:ns0="sample.com/x">
<ns0:b>Hello World</ns0:b>
</ns0:a>