我有A班:
@XmlRootElement(name = "A")
@XmlAccessorType(XmlAccessType.FIELD)
public class A {
@XmlAttribute(name = "xmlns")
private String xmlnsAttr;
public String getXmlnsAttr() {
return xmlnsAttr;
}
public void setXmlnsAttr(String xmlnsAttr) {
this.xmlnsAttr = xmlnsAttr;
}
}
如果xmlnsAttr的属性名称为'b'(或任何其他属性,但不是'xmlns'),则可以。但如果name =“xmlns”我有问题:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.w3.org/2000/09/xmldsig#", local:"A"). Expected elements are <{}A>
测试:
@Test
public void test() throws Exception {
StringReader reader = new StringReader("<A xmlns=\"http://www.w3.org/2000/09/xmldsig#\"></A>");
JAXBContext jc = JAXBContext.newInstance(A.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
A a = (A) unmarshaller.unmarshal(reader);
assert a.getXmlnsAttr() != null;
}
抱歉我的英语不好。 另见Query XDocument with xmlns attribute (namespace) 和 Jaxb: local namspacing via the xmlns attribute instead of the element prefix?
PS:如果我添加
@XmlSchema(namespace = "http://www.w3.org/2000/09/xmldsig#")
package <my package>;
我会有断言问题(a.getXmlnsAttr()为null)。