JAXB解组更改名称空间

时间:2019-02-10 02:40:00

标签: java xml jaxb

我有一个为安全性令牌构建的XML。它们包含基于WS-Trust的命名空间,但是当我解组该XML以创建RequestSecurityTokenResponseCollection对象时,它将用ns1ns2ns3等命名空间替换命名空间。< / 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;

1 个答案:

答案 0 :(得分:0)

我通过基于对象工厂构建整个安全令牌来解决此问题。使用JAXBmarshall每个元素。这为最终response

创建了必需的名称空间及其前缀。