Search code examples
javaxmlxml-parsingxmlhttprequestjaxb

How to parse XML request to java object?


<?xml version="1.0" encoding="utf-8"?>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig">
asasas
</Signature>

This is the requested XML

@XmlRootElement(name = "Signature")
public class Signature {

    private String xmlns;
    private String text;

// getter setter

@XmlAttribute(name = "xmlns")
    public String getXmlns() {
        return xmlns;
    }

    public void setXmlns(String xmlns) {
        this.xmlns = xmlns;
    }

    @XmlValue
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

But I'm getting error ---------->>>> exception------------ javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.w3.org/2000/09/xmldsig", local:"Signature"). Expected elements are <{}Signature>

Please help me out.


Solution

  • Expected element <{}Signature> has no namespace, but your XML request has, so they don't match. Change you annotation @XmlRootElement like below to specify the namespace qualification.

    @XmlRootElement(name = "Signature", namespace="http://www.w3.org/2000/09/xmldsig")