【问题标题】:JAXB Unmarshalling changes the namespacesJAXB Unmarshalling 改变了命名空间
【发布时间】:2019-02-10 02:40:00
【问题描述】:

我有一个为安全令牌构建的 XML。这些包含基于 WS-Trust 的命名空间,但是当我解组此 XML 以创建 RequestSecurityTokenResponseCollection 对象时,它会将命名空间替换为 ns1ns2ns3 等命名空间。

有没有一种方法可以在不更改输入 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;

【问题讨论】:

    标签: java xml jaxb


    【解决方案1】:

    我通过基于我的对象工厂构建整个安全令牌来解决这个问题。使用JAXBmarshall 每个元素。这为最终的response 创建了所需的命名空间及其前缀

    【讨论】:

      猜你喜欢
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 2010-12-10
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多