解组xacml响应时出错?

时间:2014-01-25 07:14:37

标签: java xml jaxb xacml3

我正在尝试使用jaxb解组xacml响应,但我收到了意外的元素错误。

这是我的主要方法:

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);

这里我传递的是xml字符串。这是xml以便更好地理解:

<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>

这是Response类:

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

我收到错误:

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>

请帮我解释为什么我会收到这个例外?

1 个答案:

答案 0 :(得分:3)

您需要执行以下操作:

  1. @XmlRootElement课程的Response注释更改为@XmlRootElement(name="Response")
  2. 使用包级别@XmlSchema注释映射命名空间限定(请参阅:http://blog.bdoughan.com/2010/08/jaxb-namespaces.html