I wrote class A with the xmlns attribute.

@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 you change the name of this attribute (say, to 'a'), then everything works fine. But I need an attribute with the name 'xmlns'. When using it, I get an error

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

Test code:

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

What can be fixed? I looked in the direction of XmlScheme , but it helped me a little: at best, I added postfix.

  • one
    This is not an attribute. This is the namespace ID. What do you want to do with it? - Anton Shchyrov
  • one
    It is not possible to create an attribute with the name xmlns, because it is a reserved attribute. Give your element a namespace and this attribute will appear automatically. - Alexander Petrov
  • @AlexanderPetrov I need to parse an xml object where the element contains xmlns. An example of such an object is presented in Example 27 at w3.org/TR/xmldsig-core2 . - Mikhail Ionkin

0