1

我正在尝试使用 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>

这是响应类:

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

请帮我弄清楚为什么我会得到这个例外?

4

1 回答 1

3

您需要执行以下操作:

  1. 将类@XmlRootElement上的注释更改为.Response@XmlRootElement(name="Response")
  2. 使用包级别@XmlSchema注释映射命名空间限定(参见: http ://blog.bdoughan.com/2010/08/jaxb-namespaces.html )
于 2014-01-25T11:27:32.383 回答