Link to home
Start Free TrialLog in
Avatar of BonnieK
BonnieK

asked on

Problems reading XML response with Coldfusion 9 XMLParse

Hello,

I am having trouble reading an XML response.  I am not sure if I can explain it well.  I am using XMLParse on the filecontent.

When I do this directly from the response from the web service I am using, it seems that I am getting just one chunk of XMLText instead of each piece of information with its node label.

<cfdump var="#xmlparse(httpResponse.filecontent)#">

If I show the filecontent on the screen, then copy and paste it into notepad and name it something.xml, then run XML Parse, I am able to access the different nodes.

<cfdump var="#xmlparse("test.xml")#">

Below is the filecontent that I am able to create an xml document with and read successfully.

Attached are images of the two cfdump calls.  I think the pictures should explain it.

I am not sure if this has to do with XMLParse or with the response data that is being returned to me.

Any help would be much appreciated.  I have been trying to figure this out for two days now and am exhausted...



<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ExecuteXMLResponse xmlns="http://rossinc.com/webservices/"><ExecuteXMLResult><method name="RS_BIN_TRANSFER"><database>_CONNECTSERVICE</database> <parameters><parameter> <name>ERROR_OCCURRED</name> <value>1</value> </parameter> <parameter> <name>XML_TAGS</name> <value>1</value> </parameter> <parameter> <name>Unknown2</name> <value></value> </parameter> </parameters><tables><table name="RS_SYS_MESSAGES"> <data> <row number="1"> <RS_MESSAGE_NUMBER>P_13349</RS_MESSAGE_NUMBER> <RS_MESSAGE_SEVERITY>0</RS_MESSAGE_SEVERITY> <RS_MESSAGE_TEXT>Validating Table IC_BIN_TRANSFER_CONTROL_VT, Reference 1</RS_MESSAGE_TEXT> <RS_SEQUENCE>1</RS_SEQUENCE> <RS_SORT_ORDER>1</RS_SORT_ORDER> </row> <row number="2"> <RS_MESSAGE_NUMBER>P_42264</RS_MESSAGE_NUMBER> <RS_MESSAGE_SEVERITY>1</RS_MESSAGE_SEVERITY> <RS_MESSAGE_TEXT>Insufficient Qty Available at Bin/MH level</RS_MESSAGE_TEXT> <RS_SEQUENCE>1</RS_SEQUENCE> <RS_SORT_ORDER>2</RS_SORT_ORDER> </row> <row number="3"> <RS_MESSAGE_NUMBER>P_02111</RS_MESSAGE_NUMBER> <RS_MESSAGE_SEVERITY>1</RS_MESSAGE_SEVERITY> <RS_MESSAGE_TEXT>Validation on RS_BIN_TRANSFER_CONTROL failed.</RS_MESSAGE_TEXT> <RS_SEQUENCE>1</RS_SEQUENCE> <RS_SORT_ORDER>3</RS_SORT_ORDER> </row> <row number="4"> <RS_MESSAGE_NUMBER>P_02973</RS_MESSAGE_NUMBER> <RS_MESSAGE_SEVERITY>1</RS_MESSAGE_SEVERITY> <RS_MESSAGE_TEXT>Update Failed</RS_MESSAGE_TEXT> <RS_SEQUENCE>0</RS_SEQUENCE> <RS_SORT_ORDER>4</RS_SORT_ORDER> </row> </data> </table> </tables></method> </ExecuteXMLResult></ExecuteXMLResponse></soap:Body></soap:Envelope>
From-text-pasted-into-xml-file.PNG
From-xmlparse-of-response.PNG
Avatar of BonnieK
BonnieK

ASKER

I ran XMLValidate against the file and it shows this error.  I am not really sure to go from here...

:1:206: cvc-elt.1: Cannot find the declaration of element 'soap:Envelope'.
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BonnieK

ASKER

YOU.  ARE.  MY. HERO!!!!!!!

Now, I just wish I understood :)  

This: <cfset newNode = xmlParse(node.xmlText)> worked perfectly.

It looks like you found xmlText and then parsed it.  I knew the issue had something to do with the conversion of the < and > to &lt; and &gt; but could not get it to work.  I was writing the response to an xml file and when I ran a find and replace on those character indications and changed them to < and > then I was able to parse it.

 I am an XML newbie - so I will need to do some research on how this works:  
2:<cfset node = doc["soap:Envelope"]["soap:Body"]["ExecuteXMLResponse"]["ExecuteXMLResult"]>

Anyway.  There was zero chance I was going to be able to get to this point without your help.  I miss the old days where I could give you points.  I have been away from EE for a while.  And I told myself if someone here helps me with this I will move to a premium account...so here I go!

You are amazing _agx_

Thank you for your time,
Bonnie


ToString: <?xml version="1.0" encoding="UTF-8"?> <ExecuteXMLResult xmlns="http://rossinc.com/webservices/">&lt;method name="RS_DECR_INVENTORY_V2"&gt;&lt;database&gt;_CONNECTSERVICE&lt;/database&gt; &lt;parameters&gt;&lt;parameter&gt; &lt;name&gt;ERROR_OCCURRED&lt;/name&gt; &lt;value&gt;1&lt;/value&gt; &lt;/parameter&gt; &lt;parameter&gt; &lt;name&gt;XML_TAGS&lt;/name&gt; &lt;value&gt;1&lt;/value&gt; &lt;/parameter&gt; &lt;parameter&gt; &lt;name&gt;Unknown2&lt;/name&gt; &lt;value&gt;&lt;/value&gt; &lt;/parameter&gt; &lt;/parameters&gt;&lt;tables&gt;&lt;table name="RS_SYS_MESSAGES"&gt; &lt;data&gt; &lt;row number="1"&gt; &lt;RS_MESSAGE_NUMBER&gt;P_10648&lt;/RS_MESSAGE_NUMBER&gt; &lt;RS_MESSAGE_SEVERITY&gt;1&lt;/RS_MESSAGE_SEVERITY&gt; &lt;RS_MESSAGE_TEXT&gt;Invalid Movement Code&lt;/RS_MESSAGE_TEXT&gt; &lt;RS_SEQUENCE&gt;0&lt;/RS_SEQUENCE&gt; &lt;RS_SORT_ORDER&gt;1&lt;/RS_SORT_ORDER&gt; &lt;/row&gt; &lt;row number="2"&gt; &lt;RS_MESSAGE_NUMBER&gt;P_02973&lt;/RS_MESSAGE_NUMBER&gt; &lt;RS_MESSAGE_SEVERITY&gt;1&lt;/RS_MESSAGE_SEVERITY&gt; &lt;RS_MESSAGE_TEXT&gt;Update Failed&lt;/RS_MESSAGE_TEXT&gt; &lt;RS_SEQUENCE&gt;0&lt;/RS_SEQUENCE&gt; &lt;RS_SORT_ORDER&gt;2&lt;/RS_SORT_ORDER&gt; &lt;/row&gt; &lt;/data&gt; &lt;/table&gt; &lt;/tables&gt;&lt;/method&gt; </ExecuteXMLResult>
--------------------------------------------------------------------------------
XMLText: <method name="RS_DECR_INVENTORY_V2"><database>_CONNECTSERVICE</database> <parameters><parameter> <name>ERROR_OCCURRED</name> <value>1</value> </parameter> <parameter> <name>XML_TAGS</name> <value>1</value> </parameter> <parameter> <name>Unknown2</name> <value></value> </parameter> </parameters><tables><table name="RS_SYS_MESSAGES"> <data> <row number="1"> <RS_MESSAGE_NUMBER>P_10648</RS_MESSAGE_NUMBER> <RS_MESSAGE_SEVERITY>1</RS_MESSAGE_SEVERITY> <RS_MESSAGE_TEXT>Invalid Movement Code</RS_MESSAGE_TEXT> <RS_SEQUENCE>0</RS_SEQUENCE> <RS_SORT_ORDER>1</RS_SORT_ORDER> </row> <row number="2"> <RS_MESSAGE_NUMBER>P_02973</RS_MESSAGE_NUMBER> <RS_MESSAGE_SEVERITY>1</RS_MESSAGE_SEVERITY> <RS_MESSAGE_TEXT>Update Failed</RS_MESSAGE_TEXT> <RS_SEQUENCE>0</RS_SEQUENCE> <RS_SORT_ORDER>2</RS_SORT_ORDER> </row> </data> </table> </tables></method>
--------------------------------------------------------------------------------
Raw Value: <?xml version="1.0" encoding="UTF-8"?> <ExecuteXMLResult xmlns="http://rossinc.com/webservices/">&lt;method name="RS_DECR_INVENTORY_V2"&gt;&lt;database&gt;_CONNECTSERVICE&lt;/database&gt; &lt;parameters&gt;&lt;parameter&gt; &lt;name&gt;ERROR_OCCURRED&lt;/name&gt; &lt;value&gt;1&lt;/value&gt; &lt;/parameter&gt; &lt;parameter&gt; &lt;name&gt;XML_TAGS&lt;/name&gt; &lt;value&gt;1&lt;/value&gt; &lt;/parameter&gt; &lt;parameter&gt; &lt;name&gt;Unknown2&lt;/name&gt; &lt;value&gt;&lt;/value&gt; &lt;/parameter&gt; &lt;/parameters&gt;&lt;tables&gt;&lt;table name="RS_SYS_MESSAGES"&gt; &lt;data&gt; &lt;row number="1"&gt; &lt;RS_MESSAGE_NUMBER&gt;P_10648&lt;/RS_MESSAGE_NUMBER&gt; &lt;RS_MESSAGE_SEVERITY&gt;1&lt;/RS_MESSAGE_SEVERITY&gt; &lt;RS_MESSAGE_TEXT&gt;Invalid Movement Code&lt;/RS_MESSAGE_TEXT&gt; &lt;RS_SEQUENCE&gt;0&lt;/RS_SEQUENCE&gt; &lt;RS_SORT_ORDER&gt;1&lt;/RS_SORT_ORDER&gt; &lt;/row&gt; &lt;row number="2"&gt; &lt;RS_MESSAGE_NUMBER&gt;P_02973&lt;/RS_MESSAGE_NUMBER&gt; &lt;RS_MESSAGE_SEVERITY&gt;1&lt;/RS_MESSAGE_SEVERITY&gt; &lt;RS_MESSAGE_TEXT&gt;Update Failed&lt;/RS_MESSAGE_TEXT&gt; &lt;RS_SEQUENCE&gt;0&lt;/RS_SEQUENCE&gt; &lt;RS_SORT_ORDER&gt;2&lt;/RS_SORT_ORDER&gt; &lt;/row&gt; &lt;/data&gt; &lt;/table&gt; &lt;/tables&gt;&lt;/method&gt; </ExecuteXMLResult>
xml-parse-of-node-xmltext.PNG
Avatar of BonnieK

ASKER

Thank you so much.
Avatar of BonnieK

ASKER

Looks like we still can give points :)
Very glad I could help :)

It looks like you found xmlText and then parsed it.
Yep.  Since xmlParse did not convert that particular text, my guess is the sender is deliberately escaping it somehow to prevent it from being interpreted. For example, they might be marking it as CDATA (character data or character literal).  So the solution is to grab the node's text and xmlparse it separately.

I am an XML newbie - so I will need to do some research on how this works:  
2:<cfset node = doc["soap:Envelope"]["soap:Body"]["ExecuteXMLResponse"]["ExecuteXMLResult"]>

For the most part, XML nodes are just like nested CF structures you can access with the right combination of key names. The "name" of each node in the xml, becomes the name of a structure:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope ...>  <=== (Name "soap:Envelope" becomes the root structure)
- <soap:Body> <=== (Name "soap:Body" becomes a child structure)
--- <ExecuteXMLResponse> <=== (Name "ExecuteXMLResponse" becomes a child structure)
... etc...

Ignoring the "soap:" prefix for a moment, the xmlParse result will be similar to creating nested structures like this with CFML:

        <cfset myStruct.Envelope.body.ExecuteXMLResponse.ExecuteXMLResult = "more objects here">
        <cfdump var="#myStruct#">

To get a specific node, use regular dot notation or structure notation

      #myStruct.Envelope#
      #myStruct.Envelope.body#
      #myStruct.Envelope.body.ExecuteXMLResponse#
      .... OR ....
      #myStruct["Envelope"]#
      #myStruct["Envelope"]["body"]#
      #myStruct["Envelope"]["body"]["ExecuteXMLResponse"]#

So getting back to the xmlParse, it basically converts the input string into a bunch of nested structures that you see in the dump.

   <cfset doc = xmlparse(trim(filecontent))>
   <cfdump var="#doc#">

Since the some of the key names contains invalid characters, like colon ":", you must use structure notation to access the nodes. To grab the top level node "<soap:Envelope>", use the key name "soap:Envelope" (do not include the < and >)

   <cfset envelopeNode = doc['soap:Envelope']>
   <cfdump var="#envelopeNode#">

To get the next level down, </soap:Body>, just add the child key "soap:Body"

   <cfset bodyNode = doc['soap:Envelope']['soap:Body']>
   <cfdump var="#bodyNode#">

... and so on.