Solved

Modify xml to string with coldfusion

Posted on 2012-04-07
12
745 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 500 total points
ID: 37820665
<!--- 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
ID: 37821024
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_
ID: 37821430
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:LeadCo
ID: 37821592
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_
ID: 37821657
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
ID: 37822305
string separated by new lines.
0
 
LVL 2

Expert Comment

by:sumCold
ID: 37822601
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
ID: 37823433
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_
ID: 37839812
How did you make out?
0
 

Author Closing Comment

by:LeadCo
ID: 37840900
Thanks for all the Help! The solution worked perfectly.
0
 

Author Comment

by:LeadCo
ID: 37840942
<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_
ID: 37842431
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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