|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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> |
|
#4
|
|||
|
|||
|
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.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > Coldfusion and XML |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|