Created
April 21, 2010 13:43
-
-
Save louis-wu/373817 to your computer and use it in GitHub Desktop.
High-Performance XML Parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0'?> | |
<!-- | |
! Excerpted from "Enterprise Recipes for Ruby and Rails" | |
! xml file containing credit card transaction. | |
! 应用的目录为: ccdemo/data/cc_xactions/20080729.xml | |
--> | |
<cc-xactions date='20080729'> | |
<cc-xaction id='100001' cc-ref='2537403' type='credit' amount='12.00'> | |
<text>Monthly bill.</text> | |
</cc-xaction> | |
<!-- ... --> | |
<cc-xaction id='400224' cc-ref='95932' type='purchase' amount='19.99'> | |
<text>A new book.</text> | |
</cc-xaction> | |
</cc-xactions> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
! The Model for credit card transaction | |
! 应用的目录为:/ccdemo/app/models/credit_card_transaction.rb | |
--> | |
require 'xml/libxml' | |
class CreditCardTransaction | |
XACTION_DIR = File.join('data', 'cc_xactions') | |
attr_reader :xaction_id, :cc_ref, :type, :amount, :text | |
def initialize(xaction_id, cc_ref, type, amount, text) | |
@xaction_id, @cc_ref, @type = xaction_id, cc_ref, type | |
@amount, @text = amount, text | |
end | |
def self.find_all(cc_ref) | |
xactions = [] | |
input_file = "#{XACTION_DIR}/xactions.xml" | |
doc = XML::Document.file(input_file) | |
doc.find('//cc-xactions/cc-xaction').each do |node| | |
if node['cc-ref'] == cc_ref | |
xactions << CreditCardTransaction.new( | |
node['id'], | |
node['cc-ref'], | |
node['type'], | |
node['amount'], | |
node.find_first('text') | |
) | |
end | |
end | |
xactions | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
! The controller for finding all credit card transactions | |
! 应用的目录为:ccdemo/app/controllers/credit_card_transaction_controller.rb | |
--> | |
class CreditCardTransactionController < ApplicationController | |
def show | |
@xactions = CreditCardTransaction.find_all(params[:id]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
! The View looks as follows | |
! 应用的目录为:ccdemo/app/views/credit_card_transaction/show.html.erb | |
--> | |
<% if @xactions.size == 0 %> | |
<p>Currently, there are no transactions.</p> | |
<% else %> | |
<table> | |
<tr> | |
<th>Transaction ID</th> | |
<th>Credit Card Reference</th> | |
<th>Amount</th> | |
<th>Text</th> | |
</tr> | |
<% for xaction in @xactions %> | |
<tr> | |
<td><%= xaction.xaction_id %></td> | |
<td><%= xaction.cc_ref %></td> | |
<% sign = (xaction.type == 'purchase') ? '+' : '-' %> | |
<td><%= sign + number_to_currency(xaction.amount) %></td> | |
<td><%= xaction.text %></td> | |
</tr> | |
<% end %> | |
</table> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
应用说明: create a Rails application that scans through an XML file containing credit card transactions and displays all transactions belonging to a particular credit card.