B
Bucco
So, I'm reading this document on XML and using XML as a database. Of
course the author uses this cryptic perl script to parse the xml file:
#!/usr/bin/perl
use XML::LibXML;
my $parser = new XML::LibXML;
my $doc = $parser->parse_file( shift @ARGV );
my $balance = $doc->findvalue( '/checkbook/@balance-start' );
foreach my $record ( $doc->findnodes( '//debit' )) {
$balance -= $record->findvalue( 'amount' );
}
foreach my $record ( $doc->findnodes( '//deposit' )) {
$balance += $record->findvalue( 'amount' );
}
print "Current balance: $balance\n";
So, since I was trying to figure out how to use xml as a database and
how to use REXML I gave the script a whack and tried to write a ruby
script to do the same thing. Below is a sample of the xml file and the
ruby script:
<?xml version="1.0"?>
<checkbook balanceStart="2460.62">
<title>expenses: january 2002</title>
<debit category="clothes">
<amount>31.19</amount>
<date><year>2002</year><month>1</month><day>3</day></date>
<payto>Walking Store</payto>
<description>shoes</description>
</debit>
<deposit category="salary">
<amount>1549.58</amount>
<date><year>2002</year><month>1</month><day>7</day></date>
<payor>Bob's Bolts</payor>
</deposit>
</checkbook>
#!/usr/bin/ruby -w
require 'rexml/document'
# Read in XML doc
doc = REXML:
ocument.new(File.open('cb.xml'))
# Future version need to have entry from command line
# Find the balance and assign to float variabl 'balance'
balance = doc.root.attributes['balanceStart'].to_f
# Calculate debits and balance
doc.elements.each("//debit/amount") {|o| balance -= o.text.to_f}
# Calculate deposits and balance
doc.elements.each("//deposit/amount") {|i| balance += i.text.to_f}
#Display final balance:
puts balance
Of course I was able to complete the same task as teh perl script in
ruby with less code. (Not to mention easier to read code)
Just to help me complete the learning process, I wish to pose the
question to the group: Is there a better way to do this, and is there
more optimization I can do to my code?
Thanks
SA
course the author uses this cryptic perl script to parse the xml file:
#!/usr/bin/perl
use XML::LibXML;
my $parser = new XML::LibXML;
my $doc = $parser->parse_file( shift @ARGV );
my $balance = $doc->findvalue( '/checkbook/@balance-start' );
foreach my $record ( $doc->findnodes( '//debit' )) {
$balance -= $record->findvalue( 'amount' );
}
foreach my $record ( $doc->findnodes( '//deposit' )) {
$balance += $record->findvalue( 'amount' );
}
print "Current balance: $balance\n";
So, since I was trying to figure out how to use xml as a database and
how to use REXML I gave the script a whack and tried to write a ruby
script to do the same thing. Below is a sample of the xml file and the
ruby script:
<?xml version="1.0"?>
<checkbook balanceStart="2460.62">
<title>expenses: january 2002</title>
<debit category="clothes">
<amount>31.19</amount>
<date><year>2002</year><month>1</month><day>3</day></date>
<payto>Walking Store</payto>
<description>shoes</description>
</debit>
<deposit category="salary">
<amount>1549.58</amount>
<date><year>2002</year><month>1</month><day>7</day></date>
<payor>Bob's Bolts</payor>
</deposit>
</checkbook>
#!/usr/bin/ruby -w
require 'rexml/document'
# Read in XML doc
doc = REXML:
# Future version need to have entry from command line
# Find the balance and assign to float variabl 'balance'
balance = doc.root.attributes['balanceStart'].to_f
# Calculate debits and balance
doc.elements.each("//debit/amount") {|o| balance -= o.text.to_f}
# Calculate deposits and balance
doc.elements.each("//deposit/amount") {|i| balance += i.text.to_f}
#Display final balance:
puts balance
Of course I was able to complete the same task as teh perl script in
ruby with less code. (Not to mention easier to read code)
Just to help me complete the learning process, I wish to pose the
question to the group: Is there a better way to do this, and is there
more optimization I can do to my code?
Thanks
SA