3 Replies - 7260 Views - Last Post: 25 September 2012 - 06:17 PM Rate Topic: -----

#1 accomfund  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-September 12

How to parse ColdFusion XML file display multiple children

Posted 25 September 2012 - 10:23 AM

Hello:

New to forum, fairly new to ColdFusion. I am trying to parse an XML file and get a list of the fields in the following XML file. I would like all the Course Schedule/Class Event/ClassEventID items to list under that parent all the ClassEvent children.

I am attaching the XML file (CourseInformation_sample.xml) and transcribing the ColdFusion code I am using. I also transcribed the rendered ColdFusion file The problem is I get only the first ClassEventID (and concurrent children) and am not sure how to nest a loop (?) so that I get all the ClassEventID (and concurrent children) listed under the corresponding parent element.


Code for CourseInformation_sample_read.cfm
<CFSET CourseInformation_Short = ExpandPath("CourseInformation_sample.xml")>
<cffile 
	action="read" 
	file="#CourseInformation_Short#" 
	variable="DisVarXMLFile">
<cfset WorkingXMLDoc = xmlParse(DisVarXMLFile)>

<cfset CourseNodes = xmlSearch(WorkingXMLDoc,'/CourseInformation/Courses/Course')>

<cfoutput>
<cfloop from="1" to="#arraylen(CourseNodes)#" index="i">
   <!--- The array contents need to parsed so you can easily get at the child nodes children and attributes. --->
<cfset CourseXML = xmlparse(CourseNodes[i])>
<cfset currentrow = i> [#currentrow#]<br>
	<b>Course Title:</b> #CourseXML.Course.CourseTitle.xmltext#<br>
	<b>Course Number:</b> #CourseXML.Course.CourseNumber.xmlText#
	<br><br>
	[code]<b>Class Event ID:</b> #CourseXML.Course.CourseSchedule.ClassEvent.ClassEventID.xmlText#<br>
    <b>Start Date:</b>  #CourseXML.Course.CourseSchedule.ClassEvent.StartDate.xmlText#<br>
    <b>End Date:</b>  #CourseXML.Course.CourseSchedule.ClassEvent.EndDate.xmlText#<br>
	<b>Location:</b>  #CourseXML.Course.CourseSchedule.ClassEvent.Location.xmlText#<br>
	<hr>
</cfloop>
<br>
</cfoutput>



Following is how CourseInformation_sample_read.cfm renders - note that only the first Class Event ID is listed. I have tried several different nested loops but I can't seem to get anything other than only one Class Event ID to list or else a separate list of ALL (aggregated) Class Event IDs in one big list. I have looked at more than 20 article on the Web, cannot get anything to work properly. I might be missing the obvious.

[1]
Course Title: Preparing for MCTS Windows Server 2008 Active Directory Exam 70-640
Course Number: 1110

Class Event ID: 12AE61US
Start Date: 10/31/2012
End Date: 11/02/2012
Location: Washington, DC (Reston, VA)


[2]
Course Title: Achieving ITIL Foundation Certification
Course Number: 1197

Class Event ID: 129A05US
Start Date: 09/19/2012
End Date: 09/21/2012
Location: Washington, DC (Rockville, MD)


[3]
Course Title: Cloud Computing Technologies: A Comprehensive Hands-On Introduction
Course Number: 1200

Class Event ID: 129A00CN
Start Date: 09/19/2012
End Date: 09/21/2012
Location: Ottawa

Thank you in advance.

Attached File(s)


This post has been edited by Craig328: 25 September 2012 - 10:53 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to parse ColdFusion XML file display multiple children

#2 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1947
  • View blog
  • Posts: 3,488
  • Joined: 13-January 08

Re: How to parse ColdFusion XML file display multiple children

Posted 25 September 2012 - 11:58 AM

Hello and welcome to D.I.C. accomfund.

I don't do a lot of work with XML parsing but it seems you have a good example on line 8 of what to do further down, right? You build an array called CourseNodes from the results of an XMLSearch function at the Course level...why not do the same inside the loop that starts at line 11 for the ClassEvent level and then loop over that sub array?
Was This Post Helpful? 0
  • +
  • -

#3 accomfund  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-September 12

Re: How to parse ColdFusion XML file display multiple children

Posted 25 September 2012 - 12:58 PM

View PostCraig328, on 25 September 2012 - 07:58 PM, said:

Hello and welcome to D.I.C. accomfund.

I don't do a lot of work with XML parsing but it seems you have a good example on line 8 of what to do further down, right? You build an array called CourseNodes from the results of an XMLSearch function at the Course level...why not do the same inside the loop that starts at line 11 for the ClassEvent level and then loop over that sub array?


Yes, that's what I thought and tried that technique a couple different ways but all I could seem to get was a list of ALL

View PostCraig328, on 25 September 2012 - 07:58 PM, said:

Hello and welcome to D.I.C. accomfund.

I don't do a lot of work with XML parsing but it seems you have a good example on line 8 of what to do further down, right? You build an array called CourseNodes from the results of an XMLSearch function at the Course level...why not do the same inside the loop that starts at line 11 for the ClassEvent level and then loop over that sub array?


Sorry, accidental send: Yes, that's what I thought and tried that technique (loop over a sub array) a couple different ways but all I could seem to get was a list of ALL Course Event ID's - maybe my nesting structure was off - I will try that again...
Thanks -
Was This Post Helpful? 0
  • +
  • -

#4 accomfund  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-September 12

Re: How to parse ColdFusion XML file display multiple children

Posted 25 September 2012 - 06:17 PM

[quote name='accomfund' date='25 September 2012 - 08:58 PM' timestamp='1348603135' post='1709393']

View PostCraig328, on 25 September 2012 - 07:58 PM, said:

Hello and welcome to D.I.C. accomfund.

I don't do a lot of work with XML parsing but it seems you have a good example on line 8 of what to do further down, right? You build an array called CourseNodes from the results of an XMLSearch function at the Course level...why not do the same inside the loop that starts at line 11 for the ClassEvent level and then loop over that sub array?



Okay, so I replace this:

<b>Class Event ID:</b> #CourseXML.Course.CourseSchedule.ClassEvent.ClassEventID.xmlText#<br>
<b>Start Date:</b>  #CourseXML.Course.CourseSchedule.ClassEvent.StartDate.xmlText#<br>
<b>End Date:</b>  #CourseXML.Course.CourseSchedule.ClassEvent.EndDate.xmlText#<br>
<b>Location:</b>  #CourseXML.Course.CourseSchedule.ClassEvent.Location.xmlText#<br>


with this:

<cfset ScheduleNodes = xmlSearch(WorkingXMLDoc,'/CourseInformation/Courses/Course/CourseSchedule/ClassEvent')>
<cfloop from="1" to="#arraylen(ScheduleNodes)#" index="x">
<cfset ScheduleXML = xmlparse(ScheduleNodes[x])>
<strong>Class Event ID:</strong> #ScheduleXML.ClassEvent.ClassEventID.xmlText#<br>
<strong>Start Date:</strong>  #ScheduleXML.ClassEvent.StartDate.xmlText#<br>
<strong>End Date:</strong>  #ScheduleXML.ClassEvent.EndDate.xmlText#<br>
<strong>Location:</strong>  #ScheduleXML.ClassEvent.Location.xmlText#<br><br>
</cfloop>



But that renders a list of ALL the Class Event ID info in all three of the individual courses (see below) -- can't work out how to loop within just the individual Course/CourseTitle.

Course Title: Preparing for MCTS Windows Server 2008 Active Directory Exam 70-640
Course Number: 1110

Class Event ID: 12AE61US
Start Date: 10/31/2012
End Date: 11/02/2012
Location: Washington, DC (Reston, VA)

Class Event ID: 129A05US
Start Date: 09/19/2012
End Date: 09/21/2012
Location: Washington, DC (Rockville, MD)

Class Event ID: 129A06US
Start Date: 09/26/2012
End Date: 09/28/2012
Location: Washington, DC (Alexandria, VA)

Class Event ID: 129A07US
Start Date: 09/26/2012
End Date: 09/28/2012
Location: Los Angeles

Class Event ID: 129A00CN
Start Date: 09/19/2012
End Date: 09/21/2012
Location: Ottawa

Class Event ID: 12AA14US
Start Date: 10/10/2012
End Date: 10/12/2012
Location: Washington, DC (Reston, VA)

Course Title: Achieving ITIL Foundation Certification
Course Number: 1197

Class Event ID: 12AE61US
Start Date: 10/31/2012
End Date: 11/02/2012
Location: Washington, DC (Reston, VA)

Class Event ID: 129A05US
Start Date: 09/19/2012
End Date: 09/21/2012
Location: Washington, DC (Rockville, MD)

Class Event ID: 129A06US
Start Date: 09/26/2012
End Date: 09/28/2012
Location: Washington, DC (Alexandria, VA)

Class Event ID: 129A07US
Start Date: 09/26/2012
End Date: 09/28/2012
Location: Los Angeles

Class Event ID: 129A00CN
Start Date: 09/19/2012
End Date: 09/21/2012
Location: Ottawa

Class Event ID: 12AA14US
Start Date: 10/10/2012
End Date: 10/12/2012
Location: Washington, DC (Reston, VA)

Course Title: Cloud Computing Technologies: A Comprehensive Hands-On Introduction
Course Number: 1200

Class Event ID: 12AE61US
Start Date: 10/31/2012
End Date: 11/02/2012
Location: Washington, DC (Reston, VA)

Class Event ID: 129A05US
Start Date: 09/19/2012
End Date: 09/21/2012
Location: Washington, DC (Rockville, MD)

Class Event ID: 129A06US
Start Date: 09/26/2012
End Date: 09/28/2012
Location: Washington, DC (Alexandria, VA)

Class Event ID: 129A07US
Start Date: 09/26/2012
End Date: 09/28/2012
Location: Los Angeles

Class Event ID: 129A00CN
Start Date: 09/19/2012
End Date: 09/21/2012
Location: Ottawa

Class Event ID: 12AA14US
Start Date: 10/10/2012
End Date: 10/12/2012
Location: Washington, DC (Reston, VA)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1