Solved

Modify xml to string with coldfusion

Posted on 2012-04-07
12
733 Views
Last Modified: 2012-04-13
I need take the xml below
I am using a cfc and hitting a webservice that is returning this  xml:

<?xml version="1.0" encoding="UTF-8"?>
<VehicleInfo Year="2012">
  <Make>ACURA</Make>
  <Make>ASTON MARTIN</Make>
  <Make>AUDI</Make>
  <Make>AZURE DYNAMICS</Make>
  <Make>BENTLEY</Make>
</VehicleInfo>

I need to return  this: (stripping out all xml elements)

  ACURA
  ASTON MARTIN
  AUDI
  AZURE DYNAMICS
  BENTLEY


note the "2012" in the xml will change per query the elements will remain same.

Any Help appreciated.
0
Comment
Question by:LeadCo
  • 6
  • 4
  • 2
12 Comments
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 500 total points
Comment Utility
<!--- demo data to mimic the webservice result --->
<cfsavecontent variable="webserviceXML">
<?xml version="1.0" encoding="UTF-8"?>
<VehicleInfo Year="2012">
  <Make>ACURA</Make>
  <Make>ASTON MARTIN</Make>
  <Make>AUDI</Make>
  <Make>AZURE DYNAMICS</Make>
  <Make>BENTLEY</Make>
</VehicleInfo>
</cfsavecontent>

<!--- parse into xml elements --->
<cfset xmlDoc = xmlParse( trim(webserviceXML) )>
<!--- find the "Make" nodes --->
<cfset makeNodes = xmlSearch( xmlDoc, "/VehicleInfo/Make")>

<!--- extract the make titles and put values into an array (list, or whatever) --->
<cfset makeArray = []>
<cfloop array="#makeNodes#" index="make">
      <cfset arrayAppend(makeArray, make.xmlText)>
</cfloop>

<cfdump var="#makeArray#">
0
 
LVL 2

Expert Comment

by:sumCold
Comment Utility
No need to create array after XMLSearch, it gives array, you can directly loop it
e.g.

<cfsavecontent variable="testXML">
      <?xml version="1.0" encoding="UTF-8"?>
      <VehicleInfo Year="2012">
        <Make>ACURA</Make>
        <Make>ASTON MARTIN</Make>
        <Make>AUDI</Make>
        <Make>AZURE DYNAMICS</Make>
        <Make>BENTLEY</Make>
      </VehicleInfo>
</cfsavecontent>
<cfset parXML = XMLParse(trim(testXML))>

<cfset parXMLSer = XMLSearch(parXML,"/VehicleInfo/Make/")>

<!--- After XML search we got the array sturcture --->

<cfdump var="#parXMLSer#">

<cfloop array="#parXMLSer#" index="i">
            <cfoutput>#i#<br /></cfoutput>
</cfloop>
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
No need to create array after XMLSearch, it gives array, you can directly loop it

You must not have read the comments which say "add to array, list or whatever" ;-) He didn't say what he was ultimately doing with the results... So the example shows how to access the values. Then he can easily do whatever he needs to do from there (output, store in db, send to another function, etc...)
0
 

Author Comment

by:LeadCo
Comment Utility
Sorry guys let me be more specific with what I am doing.

I have a javascript file that uses this call to load data into a drop down. This jscript is not my own and uses a different db from a different derver. So I need to mimic how the results are returned so my drop down values will be loaded without reinventing the wheel.

    xmlHTTPObj.open("GET", serverURL + "?method=method&arg1=arg1";
   xmlHTTPObj.send(null);
 

The original code uses a php page to return results from. I am using a cfc.

My cfc page on this request I have it returning the right data but its putting this in the drop down:

         ACURA <br>
        ASTON MARTIN<br>
        AUDI<br>
        AZURE DYNAMICS<br>
        BENTLEY


The <br>'s are showing up in the selection when I remove <br> nothing show up in the drop down box and the source view from the output from cfc shows data but all on one line :
ACURA ASTON MARTIN AUDI BENTLEY

and the jscript is wanting data returned like this:
ACURA
ASTON
MARTIN
AUDI
BENTLEY

I can't seem to get the line breaks .

Any ideas how to output from cfc or Return from cfc
this:
ACURA
ASTON
MARTIN
AUDI
BENTLEY

Without adding additional wddx or other html with it.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
We need to see how the javascript populates the list to know what you should be returning from the cfc  ie Does the javascript expect an array, json, a string separated by new lines, etc...
0
 

Author Comment

by:LeadCo
Comment Utility
string separated by new lines.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Expert Comment

by:sumCold
Comment Utility
What I understand according to you is your output is coming from cfc is

ACURA<br>ASTON MARTIN<br>AUDI<br>AZURE DYNAMICS<br>BENTLEY

and if you want your output in this way as shown below using java script:

ACURA
ASTON MARTIN
AUDI
AZURE DYNAMICS
BENTLEY

then use below code:

var str=" ACURA <br>ASTON MARTIN<br>AUDI<br>AZURE DYNAMICS<br>BENTLEY";

strArray = str.split('<br>');

//using this loop you can easily assign the value in select box.

for(i=0;i<strArray.length;i++){
   alert(strArray['i']);//remove single quote
}
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
Comment Utility
string separated by new lines.

I think the easiest way is add the items to an array in your cfc, like in my original example.  Then use arrayToList() to return it as a string separated by new lines (ie chr(10), etc..).  So it returns exactly what the javascript code is expecting.

ie         #arrayToList( makeArray, chr(10) )#

Edit: If you're still seeing "<br>" in the result, double check your function's returnType and returnFormat and make sure it's not altering the output. It should be something like returnType="string" and returnFormat="plain".
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
How did you make out?
0
 

Author Closing Comment

by:LeadCo
Comment Utility
Thanks for all the Help! The solution worked perfectly.
0
 

Author Comment

by:LeadCo
Comment Utility
<cfset arrayAppend(submodelArray,sub-model.xmlText)>

This causing an issue with the "-" in the coldfusion any ideas how to get around it? I have tried   <cfset arrayAppend(submodelArray,"sub-model".xmlText)>
AND   <cfset arrayAppend(submodelArray,"sub-model.xmlText")>

Does not work.
0
 
LVL 52

Expert Comment

by:_agx_
Comment Utility
This causing an issue with the "-" in the coldfusion

Sorry I didn't see this response until now.  Correct, it doesn't work because "-" is invalid character for variable names.  But you don't need it.  The code doesn't use the node name ie "Sub-Model", it uses the loop "index" which can be any name you choose, such as:

       <cfloop array="#submodelArray#" index="lookMaNoDashes">
             <cfset arrayAppend(submodelArray, lookMaNoDashes.xmlText)>
       </cfloop>
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now