ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old April 6th, 2005, 09:48 PM
corrupted_wise corrupted_wise is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Kansas
Posts: 70 corrupted_wise User rank is Private First Class (20 - 50 Reputation Level)corrupted_wise User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 12 h 52 m 6 sec
Reputation Power: 6
Coldfusion and XML

Hello,

My goal is this code is to try to lay off as many database queries as possible; and a possible solution I came up with is to parse an XML file that houses constantly maintained data as the actual SQL database. I'm dealing with a member's database file, mind you.

Ok, so here's the structure I got for the XML:
Code:
 <forum>
    <member id="1">
      <name>Administrator</name>
      <title>Admin</title>
   </member>
</forum>


Of course, that is not the real file, just an idea of what it looks like.

Now, to display the information.

Imo, what I was thinking was to load the xml file in the application.cfm file, now since the variable seems to not be able to pass to a cfc file where I have the function, I passed it through an argument. Ok, not a big deal.

Here's my function in the cfc file:
Code:
	<cffunction name="getModerators" access="remote">
		<cfset test=xmlSearch(arguments[1], "/forum/member/")>
		<cfset ModList=ArrayNew(1)>
		<cfscript>
			for (i = 1; i LTE ArrayLen(test); i = i + 1) {
				if(test[i].title.XmlText is 'Moderator') {
					ArrayAppend(ModList, "#test[i].name.XmlText#");
				}
			} // end for
			ArraySort(ModList, "textnocase");
		</cfscript>
		<cfreturn ModList>
	</cffunction>

That will be the list of functions, where the caller will simply cfloop through it and print it.

Now this works, the problem is when I turn on the debugging settings, I see that it takes 5 times longer than a normal SQL query.

I'm dealing with about 400 members, with each xml row having 10 attributes foreach member. I'm almost about to turn to javascript, but am I going about this the wrong way? Is there a faster way to parse the XML?

I'm running the latest Coldfusion 7.0, on a IIS 4.0 win2k server.

EDIT: Well, after taking some more stabs at it, I tried storing my XML variable into an application variable, instead of an argument, and it cut the load times in half. Still not to where I would hope it to be. I believe my main issue now lies with the function component itself.

Reply With Quote
  #2  
Old April 7th, 2005, 08:24 AM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,627 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 8 m 55 sec
Reputation Power: 53
To start off, you should always use <cfargument> to declare function arguments. It enforces type checking and makes the function much more clear.

Code:
	<cffunction name="getModerators" access="remote">
		<cfargument name="theXML" type="XML" required="true" />
		<cfset test=xmlSearch(arguments.theXML, "/forum/member/")>
		<cfset ModList=ArrayNew(1)>
		<cfscript>
			for (i = 1; i LTE ArrayLen(test); i = i + 1) {
				if(test[i].title.XmlText is 'Moderator') {
					ArrayAppend(ModList, "#test[i].name.XmlText#");
				}
			} // end for
			ArraySort(ModList, "textnocase");
		</cfscript>
		<cfreturn ModList>
	</cffunction>


Storing the XML in the application scope is one option to speed things up so that you don't have to read from disk on every request. But then you have issues with caching becuase if the XML on disk changes, how do you keep the application-scoped cache of the XML updated?

There are security issues with XML. If you have the XML stored in your web root then any user could view that XML. Depending on what it is this may be a problem.

Finally, you are looping over the XML subset which, depending on its size, may be a serious performance drag. In all, unless you have a very good reason to not use a database, I'd stick with a database. Persisting data is all they are for, and they have been highly optimized for that purpose. Basiclly it will almost always be faster to hit the database than manipulate XML in memory, especially if the XML gets long.
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian.
How to Post a Question in the Forums

Reply With Quote
  #3  
Old April 7th, 2005, 02:27 PM
corrupted_wise corrupted_wise is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Kansas
Posts: 70 corrupted_wise User rank is Private First Class (20 - 50 Reputation Level)corrupted_wise User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 12 h 52 m 6 sec
Reputation Power: 6
Thank you for the reply.

I have made some dramatic changes since last night.

To answer your questions, this xml file is rarely updated. But when a profile is updated, it will update the xml file as well to keep it maintained. The scope is a session, which is erased on browser closed, thanks to cflock. This is fine, as I only use this for name dropdowns, and now other lists. The names are hardly ever changed.

Also, the XML is stored below the webroot.

Lastly, the changes I made was to turn the XML file into a SQL database fake with the querynew functions et al. I put this in the application file, so an already parsed variable is sent to the cfc file and ready to query. Because of this, I see much speed improvements. Still a little edge to go and improvements to make. But this way I don't have to loop through the data twice. (In that sense)

Application.cfm
Code:
<cfset session.file = xmlparse(XMLFileText)>
<cfset session.db=QueryNew("id, name, title, Status") >

<cfset session.numItems=ArrayLen(session.file.forum.member)>
<cfset temp=QueryAddRow(session.db, #session.numItems#)>
<cfloop index="i" from="1" to=#session.numItems#>
	<cfscript>
		QuerySetCell(forum.db, "id", #session.file.forum.member[i].XmlAttributes.id# ,#i#);
		QuerySetCell(forum.db, "name", #session.file.forum.member[i].name.XmlText#, #i#);
		QuerySetCell(forum.db, "title", #session.file.forum.member[i].title.XmlText# ,#i#);
		QuerySetCell(forum.db, "Status", #session.file.forum.member[i].eStatus.XmlText#, #i#);
	</cfscript>
</cfloop>


CFC file
Code:
	<cffunction name="getModerators" access="remote">
		<cfquery name="modQuery" dbType="query">
		   SELECT name 
		   FROM forum.db
		   WHERE title = 'Moderator'
		</cfquery>
		<cfset ModList=ArrayNew(1)>
		<cfloop query="modQuery">
			<cfscript>
				ArrayAppend(ModList, "#modQuery.name#");
			</cfscript>
		</cfloop>
		<cfscript>ArraySort(ModList, "textnocase");</cfscript>
		<cfreturn ModList>
	</cffunction>

Reply With Quote
  #4  
Old April 7th, 2005, 03:43 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,627 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 8 m 55 sec
Reputation Power: 53
On a side note, I'm not sure what CFLOCK has to do with your session ending on browser close. All CFLOCK does is single-thread access to a block of code.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > Coldfusion and XML


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway