【发布时间】:2019-02-10 02:40:00
【问题描述】:
我有一个为安全令牌构建的 XML。这些包含基于 WS-Trust 的命名空间,但是当我解组此 XML 以创建 RequestSecurityTokenResponseCollection 对象时,它会将命名空间替换为 ns1、ns2、ns3 等命名空间。
有没有一种方法可以在不更改输入 XML 中的命名空间和命名空间前缀的情况下解组 XML?
这是我一直在尝试的方式:
class XMLReaderWithoutNamespace extends StreamReaderDelegate
{
public XMLReaderWithoutNamespace(XMLStreamReader reader)
{
super(reader);
}
@Override
public String getAttributeNamespace(int arg0)
{
return "";
}
@Override
public String getNamespaceURI()
{
return "";
}
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(inputXML);
Result outputTarget = new StreamResult(outputStream);
TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
XMLStreamReader xsr = XMLInputFactory.newFactory().createXMLStreamReader(is);
XMLReaderWithoutNamespace xr = new XMLReaderWithoutNamespace(xsr);
JAXBContext jc = JAXBContext.newInstance(RequestSecurityTokenResponseCollectionType.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<RequestSecurityTokenResponseCollectionType> root = (JAXBElement<RequestSecurityTokenResponseCollectionType>) unmarshaller
.unmarshal(new StreamSource(new StringReader(sb.toString())));
response = root.getValue();
我的 package-info.java 是
@javax.xml.bind.annotation.XmlSchema(namespace = "http://docs.oasis-open.org/ws-sx/ws-trust/200512", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package org.oasis_open.docs.ws_sx.ws_trust._200512;
【问题讨论】: