我有一个为安全性令牌构建的XML。它们包含基于WS-Trust的命名空间,但是当我解组该XML以创建RequestSecurityTokenResponseCollection
对象时,它将用ns1
,ns2
,ns3
等命名空间替换命名空间。< / p>
有没有一种方法可以在不更改输入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;
答案 0 :(得分:0)
我通过基于对象工厂构建整个安全令牌来解决此问题。使用JAXB
至marshall
每个元素。这为最终response