I have searched the web many times in hoping to find a solution to my issue parsing all nodes of an XML feed with ColdFusion without success so I thought I would see if any of you can assist.
To make the aspects of this question clear here are the specifics.
Coding language: ColdFusion 10 Data source type: XML Data source file: http://media.chp.ca.gov/sa_xml/sa.xml
Issues:
Parsing all nodes, including children, in the XML
When parsing the XML I either manage to only get each center ID and Dispatch ID with one child log node or I get center ID and Dispatch ID with every log node in XML file repeated with each Dispatch output.
Goal: Parse the full XML and return every node including child log nodes organized in the same hierarchy as the original file.
I am trying to build a public safety module that people can use to see current traffic incidents with details and locations. I have been working on this on and off for months and have worked out a good deal of the functionality but the issue that has stumped me is simply parsing the full XML to return every node using ColdFusion.
Parsing the XML and passing the returned data to a database has been very simple except that I am not getting the full set of children nodes. I have tried nested loops but then I end up getting every log detail, the entire list for every dispatch location, repeated in each dispatch instead of just the ones that belong to a particular dispatch node. Does anyone have a possible solution?
I provided a link to the XML above and here is a snippet of what the XML structure looks like.
<State>
<Center ID="SAHB">
<Dispatch ID="SACC">
<Log ID="150306SA01531">
<LogTime>"Mar 6 2015 3:00PM"</LogTime>
<LogType>"1125-Traffic Hazard"</LogType>
<Location>"Sr99 S / Sr99 S Mack Rd E Ofr"</Location>
<LocationDesc>""</LocationDesc>
<Area>"South Sac"</Area>
<ThomasBrothers>""</ThomasBrothers>
<LATLON>"38476147:121424954"</LATLON>
<LogDetails>
<details/>
<units>
<UnitTime>"Mar 6 2015 3:03PM"</UnitTime>
<UnitDetail>"Unit Assigned"</UnitDetail>
</units>
<units>
<UnitTime>"Mar 6 2015 3:03PM"</UnitTime>
<UnitDetail>"Unit Assigned"</UnitDetail>
</units>
<units>
<UnitTime>"Mar 6 2015 3:03PM"</UnitTime>
<UnitDetail>"Unit Assigned"</UnitDetail>
</units>
.....
</LogDetails>
<Log ID="150306SA01531">
<LogTime>"Mar 6 2015 3:00PM"</LogTime>
<LogType>"1125-Traffic Hazard"</LogType>
<Location>"Sr99 S / Sr99 S Mack Rd E Ofr"</Location>
<LocationDesc>""</LocationDesc>
<Area>"South Sac"</Area>
<ThomasBrothers>""</ThomasBrothers>
<LATLON>"38476147:121424954"</LATLON>
<LogDetails>
<details/>
<units>
<UnitTime>"Mar 6 2015 3:03PM"</UnitTime>
<UnitDetail>"Unit Assigned"</UnitDetail>
</units>
<units>
<UnitTime>"Mar 6 2015 3:03PM"</UnitTime>
<UnitDetail>"Unit Assigned"</UnitDetail>
</units>
<units>
<UnitTime>"Mar 6 2015 3:03PM"</UnitTime>
<UnitDetail>"Unit Assigned"</UnitDetail>
</units>
.....
</LogDetails>
......
</Log>
</Dispatch>
</Center>
Here is an example of a nested loop test I did attempting to get every log node but this only created another instance of every log node being returned within each dispatch node and not just the ones that are direct children to each dispatch node.
<pre>
<p><!--- Grab CHP XML feed --->
<cftry>
<cfhttp url="http://media.chp.ca.gov/sa_xml/sa.xml"
method="get"
resolveurl="yes"
throwonerror="yes"
timeout="7">
</cfhttp>
<cfcatch>
cfhttp failure
</cfcatch>
</cftry>
</p>
<p>
<cfoutput>
<cfset Level1XML = xmlSearch(CFHTTP.FileContent,'/State/Center')>
<cfset Level2XML = xmlSearch(CFHTTP.FileContent,'/State/Center/Dispatch')>
<cfset Level3XML = xmlSearch(CFHTTP.FileContent,'/State/Center/Dispatch/Log')>
<!---<cfdump var="#Level2XML#">--->
</p>
<p><!---Main Loop Begin--->
<cfloop from="1" to="#arraylen(Level1XML)#" index="x">
<cfset CenterXML = xmlparse(Level1XML[x])>
<span style="color:##F3C;">Count Level 1 = #x#</span>
<span style="color:##FF0000;"><strong>Center:</strong>#CenterXML.Center.XMLAttributes.ID#<br></span>
<!--- Dispatch Loop Begin--->
<cfloop from="1" to="#arraylen(Level2XML)#" index="y">
<cfset DetailsXML = xmlparse(Level2XML[y])>
<span style="color:##CC3300;">Count Level 2 = #y#</span>
<cfif CenterXML.Center.Dispatch.XMLAttributes.ID EQ DetailsXML.Dispatch.XMLAttributes.ID>
<span style="color:##00CC66;"><strong>Dispatch:</strong>#DetailsXML.Dispatch.XMLAttributes.ID#<br></span>
<cfif CenterXML.Center.Dispatch.Log.XMLAttributes.ID EQ DetailsXML.Dispatch.Log.XMLAttributes.ID>
<!---Log Loop begin--->
<cfloop from="1" to="#arraylen(Level3XML)#" index="z">
<span style="color:##00CC33;">Count Level 3 = #z#</span>
<cfset LogssXML = xmlparse(Level3XML[z])>
<strong>Log Time:</strong> #LogssXML.Log.LogTime.xmlText#<br>
<strong>Log Type:</strong> #LogssXML.Log.LogType.xmlText#<br>
<strong>Log Location:</strong> #LogssXML.Log.Location.xmlText#<br>
<strong>Log Area:</strong> #LogssXML.Log.Area.xmlText#<br>
<strong>Lat Lon:</strong> #LogssXML.Log.LATLON.xmlText#<br>
<cfif isdefined("LogssXML.Log.LogDetails.units.xmlText")>
<strong>Unit Time:</strong> #LogssXML.Log.LogDetails.units.UnitTime.xmlText#<br>
<strong>Unit Detail:</strong> #LogssXML.Log.LogDetails.units.UnitDetail.xmlText#<br><br>
</cfif>
</cfloop>
</cfif>
</cfif>
</cfloop>
</cfloop>
</cfoutput>
</pre>