You'll want to investigate Xpath for querying your XML documents.
Take a look at "Extracting data with XPath" in the CF documentation.
Microsoft has a reference guide for Xpath
The XML chapters for the book "ColdFusion 8 Web Application Construction Kit Volume 2: Application Development" are available for download on the author's website.
If you post the XML text you are trying to parse I might be able to provide a sample.
I really am lost/stuck on this.
Here is the response I get from the soap header response after parsing the cfhttp.FileContent to localscope.soapresponse i.e. <cfset localscope.soapresponse = XMLParse(cfhttp.FileContent) />.
When I output the value of # #localscope.soapresponse# AND dump the results <cfdump var="#localscope.soapresponse#"> this is what I get (only seems to work in Firefox):
You will see at the top of the page are the results "OK" (or the AuthenticateUserResult) and then a key value (or the token). What for the life of me I cannot seem to do is to be able to access/reference these values as separate vars that I can use in other code. I have tried stuff like:
But that does not seem to work either. Can you please provide me some sample code of how to solve this problem?
Thanks a million,
Calling XmlParse converts the text in cfhttp.FileContent to an object representation of an XML document. Can you post the contents of cfhttp.FileContent (without the XmlParse)?
If I don't use XMLParse and just go <cfoutput>#cfhttp.FileContent#</cfoutput> I get the following output to screen:
Above are the two results I am trying to access - the response "OK" and the token value "e1ad3dc8-9b47-48d0-a2cc-46670a031674".
If I view the source code for the page I get this:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AuthenticateUserResponse xmlns="http://services.iceportal.com/service"><AuthenticateUserResult>OK</AuthenticateUserResult> <token>e1ad3dc8-9b47-48d0-a2cc-46670a031674</token></AuthenticateUserResponse></soap:Body> </soap:Envelope>
ColdFusion does not make using XML data the utilizes name spaces as easy as it could be. I don't have the information at my fingertips, but I know that I have Google searched ColdFusion, XML and NameSpace and found some very helpful blogs and resources that explain exacty how you properly dig into the xml structure when name spaces are involved.
Take a look at the sample below. As noted by Skinner you'll need to account for namespaces when using XmlSearch. See: http://www.aftergeek.com/2006/08/xmlsearch-xpath-and-xml-namespaces-in.html
results.authenticateUserResultValue and results.tokenValue should have the values you're looking for.
<!--- soap sample data, in you case soapInfo would be the contents of cfhttp.FileContent --->
<cfsavecontent variable="soapInfo"><?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AuthenticateUserResponse xmlns="http://services.iceportal.com/service"><AuthenticateUserResult>OK</AuthenticateUserResult><token>e6384ea8-48fe-4ab3-934d-427444 6008c5</token></AuthenticateUserResponse></soap:Body></soap:Envelope></cfsavecontent>
<!--- verify soapInfo is an xml document --->
<cfset results=StructNew() />
<cfset soapXmlObj=XmlParse(soapInfo) /> <!--- convert xml document text to CF XML object --->
<!--- should get an array with one item --->
<cfset results.authenticateUserResultArray=XmlSearch(soapXmlObj, "//*[local-name()='AuthenticateUserResult']" ) />
<!--- should get an array with one item --->
<cfset results.tokenArray=XmlSearch(soapXmlObj, "//*[local-name()='token']") />
<!--- extract XmlText from array items, this will fail if arrays are empty, you may want to validate the contents of results.authenticateUserResultArray and results.authenticateUserResultArray to verify items were found --->
<cfset results.authenticateUserResultValue=results.authenticateUserResultArray.XmlText />
<cfset results.tokenValue=results.authenticateUserResultArray.XmlText />
<cfdump var="#results#" />
NOT VALID XML!
Note that the forum may break up the xml text inside the cfsavecontent tags. This may cause problems when using this sample
Message was edited by: JR "Bob" Dobbs Added comment about forum breaking up xml text.