3

如何在此处删除或重命名“ns2”前缀:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:GetCatalogResponse xmlns:ns2="https://domain.com/">
         <GetCatalogResult>result</GetCatalogResult>
      </ns2:GetCatalogResponse>
   </S:Body>
</S:Envelope>

包信息.java:

@XmlSchema(
    namespace = "https://domain.com/",
    xmlns = { 
        @XmlNs(prefix = "xxx", namespaceURI="https://domain.com/")
    },
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED
)
package com.domain.xxx;

结果:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:GetCatalogResponse xmlns:ns2="https://domain.com/">
         <GetCatalogResult>result</GetCatalogResult>
      </ns2:GetCatalogResponse>
   </S:Body>
</S:Envelope>

通过 docs @XmlNs 应该覆盖 xml 项目的默认前缀。但就我而言,它不起作用。

为什么?

4

1 回答 1

2

前缀可以这样SOAPHandler修改

SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
SOAPBody body = envelope.getBody();

Iterator iter = body.getChildElements();
while (iter.hasNext()) {
    Object object = iter.next();

    if (object instanceof SOAPElement) {
        SOAPElement element = (SOAPElement) object;
        element.removeNamespaceDeclaration(element.getPrefix());
        element.setPrefix("");
    }
}
于 2013-05-24T16:46:24.347 回答