• Post Reply Bookmark Topic Watch Topic
  • New Topic

Attaching an XML Message to the SoapBody

 
KrishnaPrasad raghavan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was wondering if there was a way to attached the contents of an xml to the SoapBody. I am using Saaj provided by Java 1.6 for this purpose. I don't want to build the message in the SoapBody,
by doing the following.



but would rather want to attach the contents of the xml directly to the SOAPBody ... Something like


is there a method provided in Saaj to do this in Java 1.6.
Thanks in advance
 
Matt Middleton
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can send an XML String as a Soap param by wrapping the XML you want to send in a CDATA tag. Something along the lines of:



This will cause the SOAP parser to read it as just a string instead of xml. On the receiving end you may have to strip the CDATA tag off though.
 
KrishnaPrasad raghavan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt,

Thanks for the quick reply.

I tried the following


But the message gets altered when it is sent. The < is replaced by & lt; (Space between & and lt; is intentional). Is there a way I can overcome this. I mean is there a way to avoid the '<' getting replaced by & lt;. I don't want to wrap the message in CData cos I would be sending the messages to the third party Service Consumer and he would'nt want the message to the wrapped in CData.

 
KrishnaPrasad raghavan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, is there a third party api that will just build me a SOAP Object by passing the Contents to the sent. I mean , I just pass the xml content to the embedded in the Soap Body and the API will give me the SOAP Message.
 
Kevin Eddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very similar problem to what I'm experiencing in my post called CDATA. I used the JAXBElement class and it included the xml inside the CDATA. However, stripping off the CDATA tag for me isn't really a viable solution.
 
KrishnaPrasad raghavan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kevin,

Please let me know, if you have found a solution to this.
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I have many examples on how to create SOAP messages using SAAJ in my SCDJWS 5 study notes document, section 5.7, which can be downloaded here: http://faq.javaranch.com/content/Exam-Objectives-5.pdf
To answer the above question, there are examples that:
  • Creating a SOAP message reading the body contents from a file
  • Create a SOAP message using DOM to read the body contents from a file

  • Best wishes!
     
    KrishnaPrasad raghavan
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    there are two to three solutions to this.

    Quick and dirty solution.

    Since most of the times the Soap Message contains the following structure.

    <SOAP-ENV:Envelope>
    <SOAP-ENV:Body>
    xml message
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>


    Just build the xml by appending the message with Envelope and Body.



    This will give the SoapMessage itself Use the Java 1.6 Api to sent it across. The message will be sent as is and the < won't be replaced with & lt;


    Secondly solution. Build the document and encode the message in the document.



    Then append the document to the Soap body.



    This would build you a soap message which can be safely sent with out problems.

    No Problems of < getting converted to & lt;

    in fact you can minimize the above code. A lot is not needed.
     
    Kevin Eddy
    Ranch Hand
    Posts: 74
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In the first solution you have
    InputStream is = new ByteArrayInputStream(message.getBytes("UTF-8"));

    I guess that "message" is your xml message string?

    In the second solution I guess the first set of code is in the buildMessge() method?
    You have:
    docBuilder.parse(new InputSource(new StringReader(sb.toString())));
    I guess that sb is a stringBuffer that represents the xml?
     
    KrishnaPrasad raghavan
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Kevin,

    You are right.

    In the first solution you have




    Exactly !!!. It is the xml message.



    Yes the StringBuffer is an xml string.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!