我正在尝试从XmlString构造XML对象,但是getUnmarshaller(element)返回null:
这是代码:
try {
InitializationService.initialize();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(new ByteArrayInputStream(xmlString.trim().getBytes()));
Element element = document.getDocumentElement();
UnmarshallerFactory unmarshallerFactory = XMLObjectProviderRegistrySupport.getUnmarshallerFactory();
org.opensaml.core.xml.io.Unmarshaller unmarshaller = unmarshallerFactory
.getUnmarshaller(element); //This is coming out be null
System.out.println(unmarshaller);
} catch (Exception e) {
e.printStackTrace();
}
这是XMLString
static String xmlString ="<?xml version=\"1.0\"?>" +
" <!DOCTYPE address" +
" [" +
" <!ELEMENT address (buildingnumber, street, city, state, zip)>" +
" <!ATTLIST address xmlns CDATA #IMPLIED>" +
" <!ELEMENT buildingnumber (#PCDATA)>" +
" <!ELEMENT street (#PCDATA)>" +
" <!ELEMENT city (#PCDATA)>" +
" <!ELEMENT state (#PCDATA)>" +
" <!ELEMENT zip (#PCDATA)>" +
" ]>" +
"" +
" <address>" +
" <buildingnumber> 29 </buildingnumber>" +
" <street> South Street</street>" +
" <city>Vancouver</city>" +
"" +
" <state>BC</state>" +
" <zip>V6V 4U7</zip>" +
" </address>";
任何帮助将不胜感激。
答案 0 :(得分:1)
OpenSAML3的初始化过程很可能存在问题。我的猜测是unmarshallerFactory
为空。首先检查您是否具有以下依赖性:
<dependency>
<groupId>org.opensaml</groupId>
<artifactId>opensaml-saml-impl</artifactId>
<version>3.0.x</version>
</dependency>
如果要尝试手动初始化OpenSAML库,就像https://github.com/wso2-extensions/carbon-identity-saml-common/blob/931e6164ee64350b15c6aa6ae080a0c5fb280828/components/org.wso2.carbon.identity.saml.common.util/src/main/java/org/wso2/carbon/identity/saml/common/util/SAMLInitializer.java#L41-L85一样,那么您将获得正确的解组器。
答案 1 :(得分:0)
我不知道是否仍然需要
DefaultBootstrap.bootstrap();
从一开始就为我解决了这个问题。