Parsing XML response from web services with ColdFusion DeserializeJSON

228 views
Skip to first unread message

hofar...@houseoffusion.com

unread,
May 9, 2013, 12:53:57 PM5/9/13
to ColdFusion Technical Talk

Hi everyone,

I'm working with ColdFusion 9.0 trying to communicate with web services that our state has set up. I use cfhttp tag to submit SOAP request which looks like this:

<cfhttp method="POST" url="http://167.21.60.200/Delaware.ICIS.XmlFiling..Services/Service.svc" useragent="gSOAP/2.8" result="httpResponse">
<cfhttpparam type="header" name="content-type" value="text/xml">
<cfhttpparam type="header" name="charset" value="utf-8">
<cfhttpparam type="header" name="content-length" value="#len(signOnSoap)#">
<cfhttpparam type="header" name="connection" value="close">
<cfhttpparam type="header" name="accept-encoding" value="no-compression" />
<cfhttpparam type="header" name="SOAPAction" value="Delaware.ICIS.XmlFiling.Services/FilingService/FileCheckAgentAccountBalanceImmediate">
<cfhttpparam type="xml" value="#trim(ckBalRequest)#">
</cfhttp>

Where ckBalRequest is an xml variable created according to the web service provider specifications. I have no problem getting the request to them and receiving the response. However, while the response should be an XML variable as well, when I tried to use XMLParse(httpReponse.FileContent), CF gave me an error: An error occured while Parsing an XML document. Content is not allowed in prolog.
So, tried cfdump to see the response I was getting. Then I got the following error: java.io.ByteArrayOutputStream.
While researching about this error, I came upon suggestion to use DeserializeJSON function for the response. Now I received this error which is something within the response:

JSON parsing failure at character 2:'-' in --uuid:e2f0c19a-df5f-4570-88f2-b641a09178ee+id=34 Content-ID: <http://tempuri.org/0> Content-Transfer-Encoding: 8bit Content-Type: application/xop+xml;charset=utf-8;type="text/xml" <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><h:agentNumber xmlns:h="Delaware.ICIS.XmlFiling.Services">9020288</h:agentNumber><h:agentPONumber xmlns:h="Delaware.ICIS.XmlFiling.Services">NoPOforThisTrans</h:agentPONumber><h:attentionLine xmlns:h="Delaware.ICIS.XmlFiling.Services">Attn: Dan's Listner</h:attentionLine><h:fileDateTime xmlns:h="Delaware.ICIS.XmlFiling.Services">20130509T11:25:50:9196</h:fileDateTime><h:packetNumber xmlns:h="Delaware.ICIS.XmlFiling.Services">9020288_20130509T1125420351</h:packetNumber><h:receivedDateTime xmlns:h="Delaware.ICIS.XmlFiling.Services">20130509T11:25:50:9196</h:receivedDateTime><h:successful xmlns:h="Delaware.ICIS.XmlFiling.Services">true</h:successful></s:Header><s:Body><checkAgentAccountBalanceResponse xmlns="Delaware.ICIS..XmlFiling.Services"><accountBalanc...


Here the actual response that the state's web service is sending me, but ColdFusion keeps having trouble seeing it as a structure:

--uuid:e2f0c19a-df5f-4570-88f2-b641a09178ee+id=34
Content-ID: <http://tempuri.org/0>
Content-Transfer-Encoding: 8bit
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:agentNumber xmlns:h="Delaware.ICIS.XmlFiling.Services">9020288</h:agentNumber>
<h:agentPONumber xmlns:h="Delaware.ICIS.XmlFiling.Services">NoPOforThisTrans</h:agentPONumber>
<h:attentionLine xmlns:h="Delaware.ICIS.XmlFiling.Services">Attn: Dan's Listner</h:attentionLine>
<h:fileDateTime xmlns:h="Delaware.ICIS.XmlFiling.Services">20130509T11:25:50:9196</h:fileDateTime>
<h:packetNumber xmlns:h="Delaware.ICIS.XmlFiling.Services">9020288_20130107T1623410102</h:packetNumber>
<h:receivedDateTime xmlns:h="Delaware.ICIS.XmlFiling.Services">20130107T16:23:41:3116</h:receivedDateTime>
<h:successful xmlns:h="Delaware.ICIS.XmlFiling.Services">true</h:successful>
</s:Header>
<s:Body>
<checkAgentAccountBalanceResponse xmlns="Delaware.ICIS.XmlFiling.Services">
<accountBalance>797250.4162</accountBalance>
<fundsReserved>254873.2338</fundsReserved>
<availableBalance>542377.1824</availableBalance>
</checkAgentAccountBalanceResponse>
</s:Body>
</s:Envelope>
--uuid:e2f0c19a-df5f-4570-88f2-b641a09178ee+id=34


When I simply display the response by placing on the page <cfoutput>#httpResponse.FileContent#</cfoutput>, I get this:

--uuid:e2f0c19a-df5f-4570-88f2-b641a09178ee+id=34 Content-ID: Content-Transfer-Encoding: 8bit Content-Type: application/xop+xml;charset=utf-8;type="text/xml" 9020288NoPOforThisTransAttn: Dan's Listner20130509T11:25:50:91969020288_20130509T112542035120130509T11:25:50:9196true789222.1662261386.2338527835.9324 --uuid:e2f0c19a-df5f-4570-88f2-b641a09178ee+id=34--


As you can see all of the headers are compressed then, and it's nearly impossible to extract the values. So I need to have the response in a structure that would give me access to the values (and so that I know what they are for me to use them correctly).

So far DeserializeJSON was the only function that actually displayed headers in somewhat XML structure, but it has trouble with unique identifier (which is a unique identifier for the session) in the beginning of response. Simply trying to remove it from the response by using ColdFusion's Replace() isn't working at all. It's still there.

I'm at wit's end at this point and desperately need experts help as to how to see the structured response from the web service. The web service developers with the state aren't helping much saying that it's ColdFusion's downside, not theirs and saying that others (such as those working with PHP apps) are able to see the structural response as it's supposed to be.

Please, help! Any input is greatly appreciated!

Sasha.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:355683

hofar...@houseoffusion.com

unread,
May 9, 2013, 1:04:12 PM5/9/13
to ColdFusion Technical Talk

Hello Sasha. Try this...

<cfset results = reReplace(httpReponse.FileContent, "^[^<]*", "", "ALL")>
<cfset results = xmlParse(results)>
<cfdump var="#results#">

Hope this helps... Che



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:355685

hofar...@houseoffusion.com

unread,
May 9, 2013, 1:16:17 PM5/9/13
to ColdFusion Technical Talk

> I'm working with ColdFusion 9.0 trying to communicate with web services that our state has set up. I use cfhttp tag to submit SOAP request which looks like this:
>
> <cfhttp method="POST" url="http://167.21.60.200/Delaware.ICIS.XmlFiling..Services/Service.svc" useragent="gSOAP/2.8" result="httpResponse">
> <cfhttpparam type="header" name="content-type" value="text/xml">
> <cfhttpparam type="header" name="charset" value="utf-8">
> <cfhttpparam type="header" name="content-length" value="#len(signOnSoap)#">
> <cfhttpparam type="header" name="connection" value="close">
> <cfhttpparam type="header" name="accept-encoding" value="no-compression" />
> <cfhttpparam type="header" name="SOAPAction" value="Delaware.ICIS.XmlFiling.Services/FilingService/FileCheckAgentAccountBalanceImmediate">
> <cfhttpparam type="xml" value="#trim(ckBalRequest)#">
> </cfhttp>

My first question is, have you tried using CFINVOKE to call the web
service? That doesn't always work, but it's specifically intended to
invoke SOAP services.

> Where ckBalRequest is an xml variable created according to the web service provider specifications. I have no problem getting
> the request to them and receiving the response. However, while the response should be an XML variable as well, when I tried to
> use XMLParse(httpReponse.FileContent), CF gave me an error: An error occured while Parsing an XML document. Content is
> not allowed in prolog.
> So, tried cfdump to see the response I was getting. Then I got the following error: java.io.ByteArrayOutputStream.
> While researching about this error, I came upon suggestion to use DeserializeJSON function for the response. Now I received
> this error which is something within the response:
>
> JSON parsing failure at character 2:'-' in --uuid:e2f0c19a-df5f-4570-88f2-b641a09178ee+id=34 ...
The reason all of the headers are compressed then is that the browser
isn't going to show you tags that it doesn't know how to parse, and
you're not outputting a well-formed XML document.

There are several problems here. First, it appears that your response
is part of a MIME multipart message, and you're including the message
headers and the boundary markers (the lines that denote the beginning
and end of the part - the two lines at the very top and bottom that
contain "--uuid ..."). You need to remove those lines, and the first
three lines after the first boundary marker which are part of the
response header and not the response body. Those lines are the content
that's not allowed in the prolog - the prolog is the part before your
root XML element, and typically contains a DTD if it exists at all.

After that - when you just have the parts that begin and end with
<s:Envelope> - you'll have a well-formed XML document. You can
probably parse that just fine with XMLParse.

> So far DeserializeJSON was the only function that actually displayed headers in somewhat XML structure, but it has trouble
> with unique identifier (which is a unique identifier for the session) in the beginning of response. Simply trying to remove it from the
> response by using ColdFusion's Replace() isn't working at all. It's still there.

I'm not sure why you'd be using DeserializeJSON for this - it's for
parsing JSON, not XML.

Dave Watts, CTO, Fig Leaf Software
http://www.figleaf.com/
http://training.figleaf.com/

Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on
GSA Schedule, and provides the highest caliber vendor-authorized
instruction at our training centers, online, or onsite.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:355687

hofar...@houseoffusion.com

unread,
May 9, 2013, 1:20:36 PM5/9/13
to ColdFusion Technical Talk

Hi Che,

Thank you for your advice, but unfortunately it didn't help. Here is an
error I got:

An error occured while Parsing an XML document.
Element or attribute do not match QName production:
QName::=(NCName':')?NCName.



Sincerely,

Sasha.
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:355688

hofar...@houseoffusion.com

unread,
May 9, 2013, 1:42:03 PM5/9/13
to ColdFusion Technical Talk
Hi Dave,

Thank you for your response. I've tried cfinvoke many times before, but all I'm getting is this error: Cannot generate stub objects for web service invocation.
After I've researched this error, I found no viable solution and I was recommended to use cfhttp posts to the web service instead.
So, when using cfhttp tag instead I was actually able to connect to their web services and receive a response back, which I couldn't with cfinvoke at all.

Next, I do realize that I need to remove the two lines at the very top and bottom that contain "--uuid ...") and the first three lines after the first boundary marker which are part of the response header. The problem here is that simply using Replace() function to replace those lines with nothing isn't working.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:355691

hofar...@houseoffusion.com

unread,
May 9, 2013, 1:57:34 PM5/9/13
to ColdFusion Technical Talk

> Next, I do realize that I need to remove the two lines at the very top and bottom that contain "--uuid ...") and the first three lines
> after the first boundary marker which are part of the response header. The problem here is that simply using Replace() function to
> replace those lines with nothing isn't working.

The Replace function requires that you specify a pattern. There are a
couple of other ways you could do this:
- replace everything before and after <s:Envelope> using simple string functions
- treat the response as a list of lines and use list functions to
remove the lines you don't want.

Dave Watts, CTO, Fig Leaf Software
http://www.figleaf.com/
http://training.figleaf.com/

Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on
GSA Schedule, and provides the highest caliber vendor-authorized
instruction at our training centers, online, or onsite.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Order the Adobe Coldfusion Anthology now!
http://www.amazon.com/Adobe-Coldfusion-Anthology/dp/1430272155/?tag=houseoffusion
Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:355694

hofar...@houseoffusion.com

unread,
May 9, 2013, 2:04:50 PM5/9/13
to ColdFusion Technical Talk

I had very similar problems with the BING AdCenter API which I and several
others never could get working. Almost identical errors. I even sent over
the raw responses from Fiddler and Microsoft technical support response to
me was the same thing. "It's one of CF's downfalls". Now I'm going to
have to go back try again :) Round two.

Matt


-----Original Message-----
From: Dave Watts [mailto:dwa...@figleaf.com]
Sent: Thursday, May 09, 2013 12:58 PM
To: cf-talk
Subject: Re: Parsing XML response from web services with ColdFusion
DeserializeJSON


Archive: http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:355695
Reply all
Reply to author
Forward
0 new messages