Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use default namespace in valid SOAP SAAJ envelope

Are these two soap messages valid ? The different part is the namespace attribute xmlns="http://www.sigvalue.com/acc". The first soap is a sample, the second one generated by java code to make the same soap message.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
  <GetNGPList xmlns="http://www.sigvalue.com/acc">
  <UserData>
    <senderId>string</senderId>
    <channelId>string</channelId>
    <timeStamp>dateTime</timeStamp>
  </UserData>
  <zipCode>string</zipCode>
</GetNGPList>
 </soap:Body>
</soap:Envelope>

.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetNGPList>
  <UserData xmlns="http://www.sigvalue.com/acc">
    <senderId>string</senderId>
    <channelId>string</channelId>
    <timeStamp>dateTime</timeStamp>
  </UserData xmlns="http://www.sigvalue.com/acc">
  <zipCode>string</zipCode>
</GetNGPList>
</soap:Body>
</soap:Envelope>

If the second soap is invalid, how could I make it the same as the first one? GetNGPList.addNamespaceDeclaration("xmlns","http://www.sigvalue.com/acc"); this line doesn't work as expected. Here is the JAVA code :

    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();

    SOAPPart soapPart = soapMessage.getSOAPPart();


    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("xsi", gXSIServerURI);
    envelope.addNamespaceDeclaration("xsd", gXSDServerURI);

        // SOAP Body
    SOAPBody soapBody = envelope.getBody();
    SOAPElement GetNGPList =  soapBody.addChildElement("GetNGPList");
    GetNGPList.addNamespaceDeclaration("xmlns","http://www.sigvalue.com/acc");

    SOAPElement UserData = GetNGPList.addChildElement("UserData");
    SOAPElement senderId = UserData.addChildElement("senderId");
    SOAPElement channelId = UserData.addChildElement("channelId");
    SOAPElement timeStamp = UserData.addChildElement("timeStamp");
    senderId.addTextNode("string");
    channelId.addTextNode("string");
    timeStamp.addTextNode("dateTime");

    SOAPElement zipCode = GetNGPList.addChildElement("zipCode");
    zipCode.addTextNode("string"); 


    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction",  "sample");

    soapMessage.saveChanges();

    /* Print the request message */
    //System.out.print("Request SOAP Message = ");
    soapMessage.writeTo(System.out);
like image 361
Frank Avatar asked Jul 10 '13 17:07

Frank


People also ask

What is SOAP envelope namespace?

SOAP defines two namespaces: The SOAP envelope, the root element of a SOAP message, has the following namespace identifier: "http://schemas.xmlsoap.org/soap/envelope" The SOAP serialization, the URI defining SOAP's serialization rules, has the following namespace identifier: "http://schemas.xmlsoap.org/soap/encoding"

How do you make a SOAP envelope in Java?

SOAPEnvelope envelope = soapPart. getEnvelope(); You can now use the getHeader and getBody methods of envelope to retrieve its empty SOAPHeader and SOAPBody objects. SOAPHeader header = envelope.

What is namespace URI in SOAP?

The namespace URI specifies the XML namespace used to compose the SOAP request envelope for the given SOAP operation. The domain component from URL clause is used when the namespace URI is not defined.

What is the need of envelope element in the SOAP document?

The required SOAP Envelope element is the root element of a SOAP message. This element defines the XML document as a SOAP message.


1 Answers

To set a namespace as the default namespace, simply use the empty string ("") as prefix name:

SOAPElement GetNGPList =
       soapBody.addChildElement("GetNGPList", "", "http://www.sigvalue.com/acc");
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The code above will apply xmlns="http://www.sigvalue.com/acc" to GetNGPList.

Your code, adapted:

// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement GetNGPList =
       soapBody.addChildElement("GetNGPList", "", "http://www.sigvalue.com/acc");

SOAPElement UserData = GetNGPList.addChildElement("UserData");
...

As usual, when you omit the namespace prefix declaration in addChildElement(), the child inherits its namespace from its parent.

The code above will generate what you needed:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
        <GetNGPList xmlns="http://www.sigvalue.com/acc">
            <UserData>
                <senderId>string</senderId>
...
like image 121
acdcjunior Avatar answered Nov 14 '22 23:11

acdcjunior