5 Replies - 3552 Views - Last Post: 14 April 2009 - 12:23 PM Rate Topic: -----

#1 xheartonfire43x   User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Looping through an XML document and creating an array from a tag

Post icon  Posted 13 April 2009 - 11:49 AM

I am trying to parse through an XML document and add all of the src attributes of an img tag t oan array.

1.xml
<?xml version="1.0" encoding="UTF-8"?>
<gallery>
	<album title="Rudy's Limo" description="Rudy's Limousine Service">
		<img src="../Admin/xmleditor/Images/Home/drivingforward.jpg" />
		<img src="../Admin/xmleditor/Images/Home/drivingforward2.jpg" />
		<img src="../Admin/xmleditor/Images/Home/drivingforward3.jpg" />
		<img src="../Admin/xmleditor/Images/Home/drivingforward4.jpg" />		
	</album>	
</gallery>



xmleditor.cfc
<cfcomponent>

<cffunction name="readXML">
	<cfargument name="id" default="" required="yes">
	<cfargument name="localXMLpath" default="" required="yes">
	
	<cfset var link = "">
	<cfset var xml = "">
	<cfset var xmlfile = arguments.localXMLpath & arguments.id & ".xml">
	
	<cffile action="read" variable="xml" file="#xmlfile#">
	<cfset xml = xmlparse(xml)>
	<cfset srcarray = arraynew(1)>
	
		
	
	<cfreturn srcarray />
</cffunction>

</cfcomponent>



index.cfm
<cfcomponent>

<cffunction name="readXML">
	<cfargument name="id" default="" required="yes">
	<cfargument name="localXMLpath" default="" required="yes">
	
	<cfset var link = "">
	<cfset var xml = "">
	<cfset var xmlfile = arguments.localXMLpath & arguments.id & ".xml">
	
	<cffile action="read" variable="xml" file="#xmlfile#">
	<cfset xml = xmlparse(xml)>
	<cfset srcarray = arraynew(1)>
	
		
	
	<cfreturn srcarray />
</cffunction>

</cfcomponent>



I am using slideshow pro and trying to make an XML editor so that my clients can edit there own slide shows. But first I need to be able to read through an XML file and see what is already there.

Is This A Good Question/Topic? 0
  • +

Replies To: Looping through an XML document and creating an array from a tag

#2 sansclue   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Looping through an XML document and creating an array from a tag

Posted 14 April 2009 - 09:23 AM

There are a couple ways to do it. One is to loop through the dom array. Another is use xmlSearch to grab the img src attribute

<cfset result = xmlSearch(xmlParsed, "/gallery/album/img//@src")>


Was This Post Helpful? 0
  • +
  • -

#3 xheartonfire43x   User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: Looping through an XML document and creating an array from a tag

Posted 14 April 2009 - 10:50 AM

View Postsansclue, on 14 Apr, 2009 - 08:23 AM, said:

There are a couple ways to do it. One is to loop through the dom array. Another is use xmlSearch to grab the img src attribute

<cfset result = xmlSearch(xmlParsed, "/gallery/album/img//@src")>



I figured all of that out from sitepoint.com. I got it working by doing a loop from 1 to the array len and puling the src using different cfsets. Now though I am having problems resizing the image and then doing a write to browser.

		<cfloop from="1" to="#arraylen(xml.gallery.album.xmlChildren)#" index="i">
			<cfset img = xml.gallery.album.xmlchildren[i].xmlattributes.src>
			<cfset imglen = len(img)>
			<cfset subtract = len('../Admin/xmleditor/')>
			<cfset imgchar = imglen - subtract>
			<cfset newimg = right(img,imgchar)>
			<cfset imageresize(newimg,"150","")>
				<p />
			<tr>
				<td>
					<cfimage action="writetobrowser" source="#newimg#" />
				</td><!---




error

Quote

Unable to cast an object of type java.lang.String to Image.


The error occurred in C:\TestingSites\Rudys Limo\Admin\xmleditor\index.cfm: line 32

30 : <cfset newimg = right(img,imgchar)>
31 : New IMG:<cfdump var="#newimg#"><br />
32 : <cfset imageresize(newimg,"150","")>
33 : <p />
34 : <tr>

This post has been edited by xheartonfire43x: 14 April 2009 - 10:51 AM

Was This Post Helpful? 0
  • +
  • -

#4 sansclue   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Looping through an XML document and creating an array from a tag

Posted 14 April 2009 - 11:19 AM

ImageResize expects an image object. You just forgot to create the image from the path.

View Postxheartonfire43x, on 14 Apr, 2009 - 09:50 AM, said:

[code]
<cfloop from="1" to="#arraylen(xml.gallery.album.xmlChildren)#" index="i">
<cfset img = xml.gallery.album.xmlchildren[i].xmlattributes.src>
<cfset imglen = len(img)>
<cfset subtract = len('../Admin/xmleditor/')>
<cfset imgchar = imglen - subtract>
<cfset newimg = right(img,imgchar)>
[code]


Can there be more than one <album>? Also, if you know the string to replace is always '../Admin/xmleditor/', why not just use replaceNoCase(...)? That would do it in one step.

This post has been edited by sansclue: 14 April 2009 - 11:19 AM

Was This Post Helpful? 0
  • +
  • -

#5 xheartonfire43x   User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: Looping through an XML document and creating an array from a tag

Posted 14 April 2009 - 12:01 PM

View Postsansclue, on 14 Apr, 2009 - 10:19 AM, said:

ImageResize expects an image object. You just forgot to create the image from the path.


Can there be more than one <album>? Also, if you know the string to replace is always '../Admin/xmleditor/', why not just use replaceNoCase(...)? That would do it in one step.


I didn't know about the replaceNoCase() function. Thanks. I changed it to that and added the imagenew() function and now it works.
Was This Post Helpful? 0
  • +
  • -

#6 sansclue   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 316
  • Joined: 21-November 07

Re: Looping through an XML document and creating an array from a tag

Posted 14 April 2009 - 12:23 PM

Good.

Just fyi, if there can be more than one <album> the xml.gallery.album.xmlchildren syntax may not pick up the images. So something to look out for ...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1