Question

For example, please consider the following code snippet:

Scenario 1:

The company name I received as a part of SOAP response is as follows:

<Company>Amazon, Inc </Company>

Scenario 2:

Another company name I received as a part of SOAP response is as follows:

<Company>Google, Inc </Company>

Basically, from the user end I am inputting some information and based on that I am getting different company names inside the <Company> tag.

The following code shows how I am storing the response in XMLResponse variable

 <cfset XMLResponse = XmlParse(

    httpResponse.fileContent.Trim()

    ) />

The following piece of code shows how I am parsing the response and storing the content in a variable:

<cfset arrCOMPANY = XmlSearch(
             XMLResponse,
             "//*[name()='Company']"
             ) /> 

So now I have arrCOMPANY = Amazon, Inc is I happen to be in Scenario and Google, Inc otherwise.

My Question:

I have to insert these data into the database based on the company name and set an integer field to be equal to 1 if the company name is Amazon,Inc and set it to zero otherwise for all other companies.

Please let me know if I am following the right path:

I am thinking of writing two cfqueries based on the company names and hence I am thinking to place the following condition :

<cfif arrCompany eq "Amazon, Inc">

// Here I will write cfquery with an integer field = 1


Or 

<cfif arrCompany eq "Google, Inc">

// Here I will write cfquery with integer field 0

So, am I doing the right comparision, I am wondering whether the company names that I am comparing with eq sign will be actually compared or not as it contains spaces after first word ( say for example the space between Amazon, and Inc).

Please share your experience.

Thanks

Here is what I did:

I have applied trim function on specific element as well. For example:

The Value in the CompanyName variable is Amazon, Inc

<cfset CompanyName = Trim(arrCompany[1])>  // adding index 1 because  it's a complex structure

Here is how I am trying to use cfif condition:

<cfif CompanyName eq "Amazon, Inc">

    <cfset m = 1>

    <cfelse>

    <cfset m = 0>  

    </cfif> 

    <cfoutput>#m#</cfoutput>

Despite doing above, I am getting 0 as my output. Please let me know if I am comparing wrong.

Was it helpful?

Solution

<cfset CompanyName = Trim(arrCompany[1])>

If you cfdump that variable, or wrap it in xmlFormat(), you will see it contains the whole node, not just the text. It is easy to miss when using only cfoutput, because the browser treats the xml as tags, so they are not visible unless you view the source:

<cfoutput>#XMLFormat(CompanyName)#</cfoutput>

To grab only the node text use the xmlText attribute, then trim it:

<cfset CompanyName = trim( arrCompany[1].xmlText )>

Edit: Also, as mentioned in the comments, you do not need separate queries just to set a bit flag. Simply set a CF variable to 1 or 0, based on the company name, then use it in your query.

OTHER TIPS

The trim function only works on the entire XML fragment. You want to:

<cfset arrCOMPANY = trim(XmlSearch(
         XMLResponse,
         "//*[name()='Company']"
         )) />

Having said that you many want to consider

a) Having the database do the translation from string to integer
OR
b) Storing the whole extracted string
OR
c) Storing the whole XML fragment

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top