JAX-WS删除xml标记的'ns'前缀

时间:2013-05-23 12:19:19

标签: soap jax-ws

如何在此处删除或重命名'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>

package-info.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项的默认前缀。但就我而言,它不起作用。

为什么?

1 个答案:

答案 0 :(得分:1)

可以通过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("");
    }
}