如何在OpenSAML中添加简单的XML元素作为AttributeValue?

时间:2014-12-26 19:46:19

标签: java xml saml-2.0 opensaml

我尝试添加类似于以下saml的XML元素:AttributeValue,

<ABC>cde</ABC>

使用DocumentBuilderFactory获取XML元素,并使用以下代码获取XSAny类型对象。

XSAnyBuilder anyBuilder = (XSAnyBuilder)Configuration.getBuilderFactory().getBuilder(XSAny.TYPE_NAME);
XSAny any = anyBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSAny.TYPE_NAME);
XSAny xo = anyBuilder.buildObject(xmlElement);

但是我从第3行得到了错误,

Caused by: java.lang.IllegalArgumentException: local part cannot be "null" when creating a QName
    at javax.xml.namespace.QName.<init>(QName.java:246)
    at javax.xml.namespace.QName.<init>(QName.java:190)
    at org.opensaml.xml.util.XMLHelper.constructQName(XMLHelper.java:433)
    at org.opensaml.xml.AbstractXMLObject.<init>(AbstractXMLObject.java:82)
    at org.opensaml.xml.validation.AbstractValidatingXMLObject.<init>(AbstractValidatingXMLObject.java:48)
    at org.opensaml.xml.schema.impl.XSAnyImpl.<init>(XSAnyImpl.java:52)
    at org.opensaml.xml.schema.impl.XSAnyBuilder.buildObject(XSAnyBuilder.java:29)
    at org.opensaml.xml.schema.impl.XSAnyBuilder.buildObject(XSAnyBuilder.java:25)
    at org.opensaml.xml.AbstractXMLObjectBuilder.buildObject(AbstractXMLObjectBuilder.java:51)
    at org.opensaml.xml.AbstractXMLObjectBuilder.buildObject(AbstractXMLObjectBuilder.java:66)

有关于此的任何想法吗?

1 个答案:

答案 0 :(得分:0)

您无法直接将XML添加为AttributeValue,因为它会破坏Assertion XML架构。

最好的解决方案是在插入XML之前转义XML:

&lt;ABC&gt;cde&lt;/ABC&gt;