【问题标题】:JAX-WS Remove 'ns' prefix of xml tagJAX-WS 删除 xml 标签的 'ns' 前缀
【发布时间】:2013-05-23 12:19:18
【问题描述】:

如何在此处删除或重命名“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 项目的默认前缀。但就我而言,它不起作用。

为什么?

【问题讨论】:

    标签: soap jax-ws


    【解决方案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("");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      相关资源
      最近更新 更多