开发者

Parsing log files in a folder in ColdFusion

The problem is there is a folder ./log/ containing the files like:

jan2010.xml, feb2010.xml, mar2010.xml, jan2009.xml, feb2009.xml, mar2009.xml ...

each xml file would like:

<root><record name="bob" spend="20"></record>...(more records)</root>

I want to write a piece of ColdFusion code (l开发者_Python百科og.cfm) that simply parsing those xml files. For the front end I would let user to choose a year, then the click submit button. All the content in that year will be show up in separate table by month. Each table shows the total money spent for each person. like:

person   cost
bob      200
mike     300
Total    500

Thanks.


The short answer is that, if your XML is correctly formatted, you can use the XMLParse() function to get the XML into a CF data object.

Sergii pointed out that XMLParse cna take a path, so you can simply read the file directly into it and assign the results to a variable.

The data should look something like an array of structures. Use CFDUMP on your CF data object to view it and help you figure it out.


I would strongly urge you to check out Microsoft's "Log Parser" if you're on Windows. It provides essentially a SQL-like query interface to all manner of log files. There's an app called "Log Parser Lizard" which provides a GUI for testing out "queries", and then you can cfexecute logparser with the queries you come up with.

Not sure if there's an equivalent for linux.

If you're on Windows and interested in hearing more, let me know. I had it hooked into an ANT task that downloaded log files every night, parsed them, generated reports, etc. It worked very well.


Concrete example:

<CFSET year = 2011 />
<CFDIRECTORY directory="#expandpath("log")#" action="list" sort="name" name="logfiles" filter="*#year#.xml" />
<CFOUTPUT query="logfiles">
  <CFSET singlelogfile = xmlparse(directory & "/" & name) />
  <CFSET records = XmlSearch(singlelogfile, "//record") />
  <table>
    <tr><td colspan="2">Month: #left(logfiles.name,3)#</td></tr>
    <CFLOOP array="#records#" index="record">
      <tr><td>#record.XmlAttributes.name#</td><td>#record.XmlAttributes.spend#</td></tr>
    </CFLOOP>
  </table>
</CFOUTPUT>

Of course you would need to change that the year comes from FORM-Scope, sum up multiple records for each person and maybe (if you can control it) change the filename from logs to 01-2011,02-2011,03-2011,..12-2011 so that they're getting directly sorted correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜