有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

xml Java SoapMessage添加空命名空间

我正在调用第三方soap,其中每个元素都必须有一个名称空间。我从Java打电话到a。网络服务。在某些元素中,我必须包含“http:/abc.com”。其他时候,我必须包括xmlns:”。比如,

<GetYears xmlns="http://example.com">
  <oCar xmlns="">
    <make xmlns="http://example.com">Ford</make>
    <model xmlns="http://example.com">F250</make>
  </oCar>
</GetYears>

我正在使用javax。xml。肥皂*

        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();
        ...
        QName bodyName = new QName("http://example.com", "GetAircraftDueListItems");
        SOAPElement soapBodyElement = soapBody.addBodyElement(bodyName);
        QName qName = new QName("", "oCar");
        SOAPElement carEement = soapBodyElement.addChildElement(qName);

默认情况下,这将生成以下输出,该输出被服务拒绝,因为OCA上缺少命名空间“”

<GetYears xmlns="http://example.com">
  <oCar>
    <make xmlns="http://example.com">Ford</make>
    <model xmlns="http://example.com">F250</make>
  </oCar>
</GetYears>

似乎忽略了空命名空间。是否有办法强制元素包含xmlns=“”

谢谢


共 (1) 个答案

  1. # 1 楼答案

    不幸的是,似乎不可能以某种方式强制显示空的namespace。关于toString()函数,请参见java docs for QName

    The commonly accepted way of representing a QName as a String was defined by James Clark. Although this is not a standard specification, it is in common use, e.g. Transformer.setParameter(String name, Object value). This implementation represents a QName as: "{" + Namespace URI + "}" + local part. If the Namespace URI .equals(XMLConstants.NULL_NS_URI), only the local part is returned. An appropriate use of this method is for debugging or logging for human consumption.

    另一方面NULL_NS_URIjava docs for XMLConstants中定义为:

    Namespace URI to use to represent that there is no Namespace. Defined by the Namespace specification to be "".

    所以这是故意的。也许您可以通过某种方式将第三方soap配置为接受空的namespaces?否则,也许检查空的namesspaces并使用一个虚拟的或默认的namespace就可以做到这一点