Copy link to clipboard
Copied
I am wondering how to find the last occurance of a word using coldfusion to gather the data i need. I am using some of the google maps XML code and the last occurance of distance gives the total distance which is what i need. How would I be able to find the last occurance and take the data from the code and place it in a template?
Copy link to clipboard
Copied
I know you mention it's one of the Google API's XML schemas, but it would really help if you posted an extract of the XML. Otherwise people are going to have to piss about looking up the Google docs or writing some test code so they can see what the schema is, just sot hey can help you answer your question. It's not the sort of thing a lot of people are going to have to hand.
The broad answer is to use xmlSearch() to search the XML, and it sounds like the example here might help you: http://zvon.org/xxl/XPathTutorial/Output/example4.html. But I can't be more specific than that based on the info you've provided (and my lack of motivation to write code to get to a point where I can better help you).
--
Adam
Copy link to clipboard
Copied
A couple of ideas come to mind.
You could do an XMLSearch() for the distance node. This will return an ARRAY of all the nodes, so the last element [distAry[listLen(distAry)] in the array should be the desired value.
You could do an XMLSearch with an XPATH argument that specifically targets the last node. I know there is XPATH syntax to find elements by position (first, last, ect). But you would have to look it up to find exactly what the syntax is.
Copy link to clipboard
Copied
<DirectionsResponse>
<status>OK</status>
−
<route>
<summary>Hwy 7/Regional Road 7 E</summary>
−
<leg>
−
<step>
<travel_mode>DRIVING</travel_mode>
−
<start_location>
<lat>43.8334600</lat>
<lng>-79.4994300</lng>
</start_location>
−
<end_location>
<lat>43.8210000</lat>
<lng>-79.4994700</lng>
</end_location>
−
<polyline>
<points>cfpjGlffdNvBc@vDB|@G`\oFtMaAfNyAjARl@d@Xb@l@~Bj@|E</points>
<levels>B?????@????B</levels>
</polyline>
−
<duration>
<value>110</value>
<text>2 mins</text>
</duration>
−
<html_instructions>
Head <b>south</b> on <b>Basaltic Rd</b> toward <b>Jacob Keffer Pkwy</b>
</html_instructions>
−
<distance>
<value>1511</value>
<text>1.5 km</text>
</distance>
</step>
−
<step>
<travel_mode>DRIVING</travel_mode>
−
<start_location>
<lat>43.8210000</lat>
<lng>-79.4994700</lng>
</start_location>
−
<end_location>
<lat>43.8196300</lat>
<lng>-79.4989100</lng>
</end_location>
−
<polyline>
<points>gxmjGtffdNpGoB</points>
<levels>BB</levels>
</polyline>
−
<duration>
<value>36</value>
<text>1 min</text>
</duration>
<html_instructions>Turn <b>left</b> at <b>Planchet Rd</b></html_instructions>
−
<distance>
<value>158</value>
<text>0.2 km</text>
</distance>
</step>
−
<step>
<travel_mode>DRIVING</travel_mode>
−
<start_location>
<lat>43.8196300</lat>
<lng>-79.4989100</lng>
</start_location>
−
<end_location>
<lat>43.8231600</lat>
<lng>-79.4720400</lng>
</end_location>
−
<polyline>
<points>uomjGdcfdNyRg{AwEq\]wDoC}QQqBF{CVyAp@qBvAcBdDoB</points>
<levels>B????@??@?B</levels>
</polyline>
−
<duration>
<value>241</value>
<text>4 mins</text>
</duration>
<html_instructions>Turn <b>left</b> at <b>Langstaff Rd</b></html_instructions>
−
<distance>
<value>2333</value>
<text>2.3 km</text>
</distance>
</step>
−
<step>
<travel_mode>DRIVING</travel_mode>
−
<start_location>
<lat>43.8231600</lat>
<lng>-79.4720400</lng>
</start_location>
−
<end_location>
<lat>43.8738800</lat>
<lng>-79.2597800</lng>
</end_location>
−
<polyline>
−
<points>
wenjGf{`dN}UubAgH{WkAcFuAqHsBiPcCg^kAmL}LobAg@{CsAqE}@mBm@cAaGuHmBwCsAaDuAiFaFiZgBgMoA{KiA{MwAqUi@}MQiJG_GAs`@WeDg@uDai@{uDuLy{@iLi}@cJap@gCaPiFgUmAyGs`@wmCqJct@aQomA}@kFiCqRsB{M]iBOMkCaNmBaLi@uDDU]mCo@}DGAqCiRFK_BuKKE}Ism@WqAqAaEoDmI]uABO]}B
</points>
−
<levels>
B???@????@?????@?@???@?????@???????????@??????????????@?????B
</levels>
</polyline>
−
<duration>
<value>1425</value>
<text>24 mins</text>
</duration>
<html_instructions>Turn <b>left</b> at <b>Hwy 7/Regional Road 7 E</b></html_instructions>
−
<distance>
<value>18072</value>
<text>18.1 km</text>
</distance>
</step>
−
<step>
<travel_mode>DRIVING</travel_mode>
−
<start_location>
<lat>43.8738800</lat>
<lng>-79.2597800</lng>
</start_location>
−
<end_location>
<lat>43.8812800</lat>
<lng>-79.2615900</lng>
</end_location>
−
<polyline>
<points>wbxjGrlwbNgBXGN{GfA{`@vF</points>
<levels>B???B</levels>
</polyline>
−
<duration>
<value>93</value>
<text>2 mins</text>
</duration>
−
<html_instructions>
Turn <b>left</b> at <b>Main St N/ON-48 N</b> (signs for <b>Sutton</b>)
</html_instructions>
−
<distance>
<value>838</value>
<text>0.8 km</text>
</distance>
</step>
−
<step>
<travel_mode>DRIVING</travel_mode>
−
<start_location>
<lat>43.8812800</lat>
<lng>-79.2615900</lng>
</start_location>
−
<end_location>
<lat>43.8829000</lat>
<lng>-79.2506100</lng>
</end_location>
−
<polyline>
<points>_qyjG|wwbN}A_L]iEy@eD{Es\Eu@DcBlAuH</points>
<levels>B???@??B</levels>
</polyline>
−
<duration>
<value>104</value>
<text>2 mins</text>
</duration>
<html_instructions>Take the 3rd <b>right</b> onto <b>Parkway Ave</b></html_instructions>
−
<distance>
<value>924</value>
<text>0.9 km</text>
</distance>
</step>
−
<step>
<travel_mode>DRIVING</travel_mode>
−
<start_location>
<lat>43.8829000</lat>
<lng>-79.2506100</lng>
</start_location>
−
<end_location>
<lat>43.8834200</lat>
<lng>-79.2503400</lng>
</end_location>
−
<polyline>
<points>c{yjGhsubNgBu@</points>
<levels>BB</levels>
</polyline>
−
<duration>
<value>27</value>
<text>1 min</text>
</duration>
−
<html_instructions>
Turn <b>left</b> at <b>Sir Constantine Dr</b><div style="font-size:0.9em">Destination will be on the right</div>
</html_instructions>
−
<distance>
<value>62</value>
<text>62 m</text>
</distance>
</step>
−
<duration>
<value>2036</value>
<text>34 mins</text>
</duration>
−
<distance>
<value>23898</value>
<text>23.9 km</text>
</distance>
−
<start_location>
<lat>43.8334600</lat>
<lng>-79.4994300</lng>
</start_location>
−
<end_location>
<lat>43.8834200</lat>
<lng>-79.2503400</lng>
</end_location>
<start_address>Vaughan, ON, Canada</start_address>
<end_address>Markham, ON, Canada</end_address>
</leg>
<copyrights>Map data ©2011 Google</copyrights>
−
<overview_polyline>
−
<points>
cfpjGlffdNvBc@vDB|@G`\oFtMaApMyA|@Db@Ll@d@Xb@l@~Bj@|EpGoByRg{AwEq\]wDoC}QQqBF{CVyAp@qBvAcBdDoB}UubAgH{WkAcFuAqHsBiPcCg^kAmL}LobAg@{CsAqE}@mBm@cAaGuHmBwCsAaDuAiFaFiZgBgMuB_SkAiPy@cO_@yJQiJI{RDoNEgEWeDij@q{DuLy{@iLi}@cJap@gCaPiFgUmAyGs`@wmCqJct@aQomA}@kFiCqRsB{M]iBOMkCaNmBaLi@uDDU]mCo@}DGAqCiRFK_BuKKE}Ism@WqAqAaEoDmI]uABO]}BgBXGN{GfA{`@vF}A_L]iEy@eD{Es\Eu@DcBlAuHgBu@
</points>
−
<levels>
B?????@???@?@A????@??@?A???@????@?????@?@???@?????@???@??????????????????????@?????@???@???????B
</levels>
</overview_polyline>
−
<bounds>
−
<southwest>
<lat>43.8196300</lat>
<lng>-79.4994700</lng>
</southwest>
−
<northeast>
<lat>43.8834200</lat>
<lng>-79.2503400</lng>
</northeast>
</bounds>
</route>
</DirectionsResponse>
Here is the code, the last <distance> tag contains the total distance of the trip which is the only thing I am interested in.
Copy link to clipboard
Copied
When faced with new XML that I need to work with in CF here is my process: Get the sample xml into a file. Write a piece of CF code to
read the file into a var, then xmlParse() the var and CFDUMP the output. How did CF store the data that you need in the xml var? Did it make an array out of it? In which case just use arrayLen() to get to the last element. Did it make it a structure? If so, is the order of the structure elements reproducable? If so, the just iterate through them and keep saving the data until you get all the way through the list of structures - the data you want will be the last one that got stored. If the order is not reproducable, then you need some data element that will tell you what the ordering is, and just use it to determine whether you have see the highest value yet (just like writing code to do sorts, etc.). WHen you're done you'll have the data. But the most important thing to do to speed up the process is to get it into the xml var and dumped out so that you can see what you are actually working with.
-reed
Copy link to clipboard
Copied
Can you show how i would code that?
Copy link to clipboard
Copied
The eassiest example I can think of:
<cfset distAry = xmlSearch("//distance",xmlData)>
<cfdump var="#distAry[arrayLen(distAry)]#">
NOTE: Untested and unvarified code. It is meant to be enough of an example to get you looking at the documentation for the various functions and putting the correct pieces in the correct places. Because I probably have not and I would be looking at said documentation if I was writing code like this for a project I am being paid to do.