在Java中从给定XML创建OpenSAML断言

在Java中从给定XML创建OpenSAML断言,java,xml,saml,opensaml,Java,Xml,Saml,Opensaml,一段时间以来,我一直在努力解决这个问题,并开始取得进展。但是,我在将SAML2断言的字符串表示形式(XML)转换为断言对象时遇到了一些问题 看起来我得到了一个有效的org.w3c.dom.Document和适当的数据,而且我似乎得到了一个有效的SAMLObjectBuilder如果其他人遇到同样的问题,并遇到这个问题,下面是答案 仅以解组为例: String inCommonMDFile = "/data/org/opensaml/saml2/metadata/InCommon-metadat

一段时间以来,我一直在努力解决这个问题,并开始取得进展。但是,我在将SAML2断言的字符串表示形式(XML)转换为断言对象时遇到了一些问题


看起来我得到了一个有效的
org.w3c.dom.Document
和适当的数据,而且我似乎得到了一个有效的
SAMLObjectBuilder

如果其他人遇到同样的问题,并遇到这个问题,下面是答案

仅以解组为例:

String inCommonMDFile = "/data/org/opensaml/saml2/metadata/InCommon-metadata.xml";

// Initialize the library
DefaultBootstrap.bootstrap(); 

// Get parser pool manager
BasicParserPool ppMgr = new BasicParserPool();
ppMgr.setNamespaceAware(true);

// Parse metadata file
InputStream in = MetadataTest.class.getResourceAsStream(inCommonMDFile);
Document inCommonMDDoc = ppMgr.parse(in);
Element metadataRoot = inCommonMDDoc.getDocumentElement();

// Get apropriate unmarshaller
UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataRoot);

// Unmarshall using the document root element, an EntitiesDescriptor in this case
EntitiesDescriptor inCommonMD = (EntitiesDescriptor) unmarshaller.unmarshall(metadataRoot);

然后将文档实例替换为
incommonmdoc
,并查看最后的
unmarshall()
调用的结果。请注意,
unmarshall()
返回一个
对象
,您需要将其转换为适当的类型。提示:如果您不确定问题的类型,可以使用
typeof
,但请注意继承。

我们不会在此处使用
[SOLVED]
编辑问题名称。如果你已经得到了答案,请在答案的左边打上绿色的勾号-你的问题将被标记为“已解决”。@marcog我一开始尝试过,但在48小时的宽限期结束之前,我无法将自己的答案标记为已被接受,而且我一直在寻找解决方案,我觉得把问题留着而不是删除是很有意义的。你发布了一个自我回答真是太好了。太多人就这样走开了+这样做的问题和答案我都要回答!同样的问题,但我没有把这些点联系起来。解组的结果是EntityDescriptor。如何用它构建saml对象?@stu:ummarshall的结果是一个与根文档元素类型相同的对象。在本例中,它是一个EntitiesDescriptor,但在您的示例中,它可能是其他类型的SAML对象,例如断言。是的,谢谢,我最终找到了它,转换到正确的对象,瞧。这一切都奏效了,我感到十分震惊。
String inCommonMDFile = "/data/org/opensaml/saml2/metadata/InCommon-metadata.xml";

// Initialize the library
DefaultBootstrap.bootstrap(); 

// Get parser pool manager
BasicParserPool ppMgr = new BasicParserPool();
ppMgr.setNamespaceAware(true);

// Parse metadata file
InputStream in = MetadataTest.class.getResourceAsStream(inCommonMDFile);
Document inCommonMDDoc = ppMgr.parse(in);
Element metadataRoot = inCommonMDDoc.getDocumentElement();

// Get apropriate unmarshaller
UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory();
Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataRoot);

// Unmarshall using the document root element, an EntitiesDescriptor in this case
EntitiesDescriptor inCommonMD = (EntitiesDescriptor) unmarshaller.unmarshall(metadataRoot);