Java SoapMessage add empty namespace

Viewed 70

I am calling third party soap where each element must have a namespace. I am calling from Java to a .NET service. In some elements, I must include "http:/abc.com". Other times, I must include xmlns:"". For example;

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

I am using javax.xml.soap.*

        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);

By default, this produces the following output which is rejected by the service because the namespace "" is missing on oCar.

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

It appears that an empty namespace is ignored. Is there a way to force the element to include xmlns=""?

Thanks

1 Answers

Unfortunately it seems that it is not possible to somehow force to show an empty namespace. See also the java docs for QName for the toString() function:

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_URI on the other hand is defined in the java docs for XMLConstants as:

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

So this is by design. Maybe you can somehow configure the third party soap to accept empty namespaces? Otherwise maybe checking for empty namesspaces and using a dummy or default namespace could do the trick.

Related