I am trying to call a DotNet based SOAP Webservice from my Java client and the SOAP request XML contains a CDATA xml as the value in <ser:answerFile> tag. The ideal SOAP request would look something like the below:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.intel.com/">
<soapenv:Header>
<ser:SessionHeader>
<ser:SessionId>6fdd74d0-3405</ser:SessionId>
</ser:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<ser:SaveProgressForUser>
<!--Optional:-->
<ser:userName>TestUser</ser:userName>
<!--Optional:-->
<ser:answerFile><![CDATA[<AnswerFile Version="2"><HeaderInfo></HeaderInfo><ps><p pid="e32ae8d7-017b-4d36-9c4c-09f6822362b3"><qs><q qid="b4c31241-c6c9-4013-9740-4cbc520dd10a" SelectedValue="" /><repeat c="2" guid="32418f0a-7e13-40db-872d-a42e220bfc15"><qs i="0" guid="b2f16b48-ca3d-4a06-85e8-7373ead7ccfe"><q qid="7834cb57-ba6f-4063-8a02-4925079d7e04" SelectedValue="" /></qs><qs i="1" guid="cb9e34a2-ecd8-4e06-8072-a6cb682fb655"><q qid="7834cb57-ba6f-4063-8a02-4925079d7e04" SelectedValue="" /></qs></repeat></qs></p></ps></AnswerFile>]]>
</ser:answerFile>
</ser:SaveProgressForUser>
</soapenv:Body>
</soapenv:Envelope>
So, in my Java client, I declare AnswerFile as a string and while invoking the Webservice I set this string using the setter.
String answerFile = "<![CDATA[<AnswerFile Version="2"><HeaderInfo></HeaderInfo><ps><p pid="e32ae8d7-017b-4d36-9c4c-09f6822362b3"><qs><q qid="b4c31241-c6c9-4013-9740-4cbc520dd10a" SelectedValue="" /><repeat c="2" guid="32418f0a-7e13-40db-872d-a42e220bfc15"><qs i="0" guid="b2f16b48-ca3d-4a06-85e8-7373ead7ccfe"><q qid="7834cb57-ba6f-4063-8a02-4925079d7e04" SelectedValue="" /></qs><qs i="1" guid="cb9e34a2-ecd8-4e06-8072-a6cb682fb655"><q qid="7834cb57-ba6f-4063-8a02-4925079d7e04" SelectedValue="" /></qs></repeat></qs></p></ps></AnswerFile>]]>"
However, smart that Java is, it encodes the < and > within the string to < and > and my generated request contains the <ser:answerFile> with an encoded value, something like below:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.intel.com/">
<soapenv:Header>
<ser:SessionHeader>
<ser:SessionId>6fdd74d0-3405</ser:SessionId>
</ser:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<ser:SaveProgressForUser>
<!--Optional:-->
<ser:userName>TestUser</ser:userName>
<!--Optional:-->
<ser:answerFile><![CDATA[<AnswerFile Version="2"><HeaderInfo><ps><p pid="e32ae8d7-017b-4d36-9c4c-09f6822362b3"><qs><q qid="b4c31241-c6c9-4013-9740-4cbc520dd10a" SelectedValue="" /><repeat c="2" guid="32418f0a-7e13-40db-872d-a42e220bfc15"><qs i="0" guid="b2f16b48-ca3d-4a06-85e8-7373ead7ccfe"><q qid="7834cb57-ba6f-4063-8a02-4925079d7e04" SelectedValue="" /></qs><qs i="1" guid="cb9e34a2-ecd8-4e06-8072-a6cb682fb655"><q qid="7834cb57-ba6f-4063-8a02-4925079d7e04" SelectedValue="" /></qs></repeat></qs></p></ps></AnswerFile>]]>
</ser:answerFile>
</ser:SaveProgressForUser>
</soapenv:Body>
</soapenv:Envelope>
So, how can I avoid this encoding and pass the answerFile value as is (With < and >).
I cannot create a document-element object structure and set, coz the setter only accepts a string as per the WSDL.
Any help will be appreciated. Please yell at me if any info is missing in the problem description.
Like I said, I havent used the Document builder approach, instead just a trivial String object to represent the CDATA XML. An excerpt of the class can be seen below.
public static void main(String[] args) throws Exception {
try {
String username = "ServiceUser";
String password = "Help#";
String masquaradeUsername = "TestUser";
NtlmAuthenticator authenticator = new NtlmAuthenticator(username, password);
Authenticator.setDefault(authenticator);
Platform platform = new Platform();
PlatformSoap client = platform.getPlatformSoap();
String answerXML = "<![CDATA[<AnswerFile Version=\"2\"><HeaderInfo></HeaderInfo><ps><p pid=\"e32ae8d7-017b-4d36-9c4c-09f6822362b3\"><qs><q qid=\"b4c31241-c6c9-4013-9740-4cbc520dd10a\" SelectedValue=\"\" /><repeat c=\"2\" guid=\"32418f0a-7e13-40db-872d-a42e220bfc15\"><qs i=\"0\" guid=\"b2f16b48-ca3d-4a06-85e8-7373ead7ccfe\"><q qid=\"7834cb57-ba6f-4063-8a02-4925079d7e04\" SelectedValue=\"\" /></qs><qs i=\"1\" guid=\"cb9e34a2-ecd8-4e06-8072-a6cb682fb655\"><q qid=\"7834cb57-ba6f-4063-8a02-4925079d7e04\" SelectedValue=\"\" /></qs></repeat></qs></p></ps></AnswerFile>]]>";
String saveGUID = client.saveProgressForUser(masquaradeUsername, answerXML);
} catch(Exception e) {
LOGGER.info("Exception encountered####");
}
}
Hope this information helps you understand my problem better.
You have two options for solving this problem.
The first is to remove the <![CDATA[ ... ]]> escaping from the XML snippet entirely. It appears that the library class PlatformSoap already escapes the XML symbols in the content.
Since PlatformSoap excapes the string when you put it in to the XML document, if you remove the CDATA from the String, it should escape the document to:
<ser:answerFile><AnswerFile Version="2">< ......... </repeat></qs></p></ps></AnswerFile>
</ser:answerFile>
That is valid XML, and, on the far side of the connection it should parse the XML properly, and restore the original content, without the CDATA.
If the far side of the connection is well-behaved, the above should just work.
On the other hand, the far side may expect to find an actual CDATA section, at which point, you should do 2 things:
PlatformSoap class to provide a method/mechanism that forces the CDATA section around the content, and supply the un-wrapped string to that new method on PlatformSoapIn summary:
PlatformSoapIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With