Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Modify xml to string with coldfusion

Posted on 2012-04-07
12
Medium Priority
?
758 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 2000 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
Industry Leaders: 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

610 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