Question

I am trying unmarshal a xacml response using jaxb but I am getting unexpected element error.

This is my Main method:

String str="<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
                    "<Response xmlns=\"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17\">"+
                    "<Result>"+
                    "<Decision>Permit</Decision>"+
                    "<Status>"+
                    "<StatusCode Value=\"urn:oasis:names:tc:xacml:1.0:status:ok\"/>"+
                    "</Status>"+
                    "<Obligations>"+
                    "<Obligation ObligationId=\"permitRule1Obligation\">"+
                    "<AttributeAssignment  AttributeId=\"urn:oasis:names:tc:xacml:2.0:example:attribute:text\" DataType=\"http://www.w3.org/2001/XMLSchema#string\">permitRule1</AttributeAssignment>"+
                    "</Obligation>"+
                    "<Obligation ObligationId=\"permitPolicyObligation\">"+
                    "<AttributeAssignment  AttributeId=\"urn:oasis:names:tc:xacml:2.0:example:attribute:text\" DataType=\"http://www.w3.org/2001/XMLSchema#string\">permitObligation</AttributeAssignment>"+
                    "</Obligation>"+
                    "</Obligations>"+
                    "</Result>"+
                    "</Response>";
        StringBuffer buf = new StringBuffer(str);
        JAXBContext jc = JAXBContext.newInstance("com.test.response1");
        Unmarshaller u = jc.createUnmarshaller();
        Response response = (Response)(u.unmarshal(new ByteArrayInputStream(buf.toString().getBytes())));
        String des= response.getResult().getDecision();
        System.out.println("Decision is: "+des);

Here I am passing xml string. Here is the xml for better understanding:

<Response xmlns="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17">
    <Result>
        <Decision>Deny</Decision>
        <Status>
            <StatusCode Value="urn:oasis:names:tc:xacml:1.0:status:ok" />
        </Status>
        <Obligations>
            <Obligation ObligationId="denyRule3Obligation">
                <AttributeAssignment
                    AttributeId="urn:oasis:names:tc:xacml:2.0:example:attribute:text"
                    DataType="http://www.w3.org/2001/XMLSchema#string">denyRule3</AttributeAssignment>
            </Obligation>
            <Obligation ObligationId="denyPolicyObligation">
                <AttributeAssignment
                    AttributeId="urn:oasis:names:tc:xacml:2.0:example:attribute:text"
                    DataType="http://www.w3.org/2001/XMLSchema#string">denyObligation</AttributeAssignment>
            </Obligation>
        </Obligations>
    </Result>
</Response>

Here is the Response class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Response", propOrder = {"result"})
@XmlRootElement
public class Response
{
    @XmlElement(name = "Result", required = true)
    protected Result result;


    public Result getResult()
    {
        return result;
    }

    public void setResult(Result result)
    {
        this.result= result;
    }
}

Error I am getting:

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"urn:oasis:names:tc:xacml:3.0:core:schema:wd-17", local:"Response"). Expected elements are <{}AttributeAssignment>,<{}Decision>,<{}Value>,<{}response>

Please help me out why I am getting this exception?

Was it helpful?

Solution

You need to do the following:

  1. Change the @XmlRootElement annotation on the Response class to be @XmlRootElement(name="Response").
  2. Map the namespace qualification with the package level @XmlSchema annotation (see: http://blog.bdoughan.com/2010/08/jaxb-namespaces.html)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top