2

I have class 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;
    }
}

If xmlnsAttr has attribute name 'b' (or any other, but not 'xmlns'), it is ok. But if name = "xmlns" I have problem:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.w3.org/2000/09/xmldsig#", local:"A"). Expected elements are <{}A>

Test:

@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;
}

Sorry for my bad English. See also Query XDocument with xmlns attribute (namespace) and Jaxb: local namspacing via the xmlns attribute instead of the element prefix?

PS: if I add

@XmlSchema(namespace = "http://www.w3.org/2000/09/xmldsig#")
package <my package>; 

I will have assertation problem (a.getXmlnsAttr() is null).

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Mikhail Ionkin
  • 568
  • 4
  • 20

0 Answers0