Modify xml to string with coldfusion

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.
LeadCoAsked:
Who is Participating?
 
_agx_Commented:
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
 
_agx_Commented:
<!--- 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
 
sumColdCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
_agx_Commented:
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
 
LeadCoAuthor Commented:
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
 
_agx_Commented:
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
 
LeadCoAuthor Commented:
string separated by new lines.
0
 
sumColdCommented:
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
 
_agx_Commented:
How did you make out?
0
 
LeadCoAuthor Commented:
Thanks for all the Help! The solution worked perfectly.
0
 
LeadCoAuthor Commented:
<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
 
_agx_Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.