Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Reading values from xml file

Reply
Thread Tools

Reading values from xml file

 
 
Vikash Kumar
Guest
Posts: n/a
 
      06-08-2007
I have a xml file

<?xml version="1.0" encoding="UTF-8"?>
<customerBill billerName="Vikash" billingAccountNumber="161"
startDate="06/01/2007" endDate="06/30/2007" openAmount="0.00"
totalCredits="0.00" totalDebits="624.00" amountDue="624.00"><bill
customerName="ECN-161" accountId="1" accountNumber="QQQ"
totalAmount="374.00"><ratePlans id="MMM" ratePlanTypeName="DDD"
totalAmount="238.00" totalVolume="136,000" avgRate="0.0018"><assetLine
assetId="MSFT" assetSymbol="MSFT" avgRate="0.0018" volume="136,000"
total="238.00"/></ratePlans><ratePlans id="OAQ" ratePlanTypeName="LOT"
totalAmount="136.00" totalVolume="136,000" avgRate="0.0010"><assetLine
assetId="NAS" assetSymbol="FT" avgRate="0.0" volume="136,000"
total="136.00"/></ratePlans></bill><bill customerName="FT"
accountId="OAQ" accountNumber="FTT" totalAmount="250.00"><ratePlans
id="HW" ratePlanTypeName="CSS" totalAmount="129.50" totalVolume="80,000"
avgRate="0.0016"><assetLine assetId="TICK" assetSymbol="MSFT"
avgRate="0.0016" volume="80,000" total="129.50"/></ratePlans><ratePlans
id="oFb" ratePlanTypeName="OD" totalAmount="120.50" totalVolume="80,000"
avgRate="0.0015"><assetLine assetId="XN" assetSymbol="FT"
avgRate="0.0015" volume="80,000"
total="120.50"/></ratePlans></bill><bill customerName="MBC-161"
accountId="dOAQ" accountNumber="MBC" totalAmount="0.00"/><bill
customerName="ND" accountId="ab" accountNumber="N"
totalAmount="0.00"/><bill customerName="OES-161" accountId="eb"
accountNumber="OE" totalAmount="0.00"/></customerBill>

I want to display, store values like this

require "rexml/document"
include REXML
doc = Document.new File.new("yourBill.xml")
billerName="";billingAccountNumber="";startDate="" ;endDate="";openAmount="";totalCredits="";totalDeb its="";
amountDue="";
root = doc.root
billerName = root.attributes['billerName']
billingAccountNumber = root.attributes['billingAccountNumber']
startDate = root.attributes['startDate']
endDate = root.attributes['endDate']
openAmount = root.attributes['openAmount']
totalCredits = root.attributes['totalCredits']
totalDebits = root.attributes['totalDebits']
amountDue = root.attributes['amountDue']

puts "Biller Name: " + billerName
puts "Acct.# " + billingAccountNumber
puts "Start Date : " + startDate
puts "End Date : " + endDate
puts "Open Amt. : " + openAmount
puts "Total Credits : " + totalCredits
puts "Total Debits : " + totalDebits
puts "Amount Due : " + amountDue

entire_details = root.elements.to_a("//bill")
puts entire_details
puts entire_details.length

puts entire_details[4].attributes['accountId']
puts
entire_details[2][0].elements["//bill/ratePlans"].attributes["totalAmount"]

I produces output like this:
Biller Name: Vikash
Acct.# 161
Start Date : 06/01/2007
End Date : 06/30/2007
Open Amt. : 0.00
Total Credits : 0.00
Total Debits : 624.00
Amount Due : 624.00
<bill accountId='1' customerName='ECN-161' accountNumber='QQQ'
totalAmount='374.00'><ratePlans avgRate='0.0018' totalVolume='136,000'
ratePlanTypeName='DDD' id='MMM' totalAmount='238.00'><assetLine
assetId='MSFT' avgRate='0.0018' total='238.00' assetSymbol='MSFT'
volume='136,000'/></ratePlans><ratePlans avgRate='0.0010'
totalVolume='136,000' ratePlanTypeName='LOT' id='OAQ'
totalAmount='136.00'><assetLine assetId='NAS' avgRate='0.0'
total='136.00' assetSymbol='FT' volume='136,000'/></ratePlans></bill>
<bill accountId='OAQ' customerName='FT' accountNumber='FTT'
totalAmount='250.00'><ratePlans avgRate='0.0016' totalVolume='80,000'
ratePlanTypeName='CSS' id='HW' totalAmount='129.50'><assetLine
assetId='TICK' avgRate='0.0016' total='129.50' assetSymbol='MSFT'
volume='80,000'/></ratePlans><ratePlans avgRate='0.0015'
totalVolume='80,000' ratePlanTypeName='OD' id='oFb'
totalAmount='120.50'><assetLine assetId='XN' avgRate='0.0015'
total='120.50' assetSymbol='FT' volume='80,000'/></ratePlans></bill>
<bill accountId='dOAQ' customerName='MBC-161' accountNumber='MBC'
totalAmount='0.00'/>
<bill accountId='ab' customerName='ND' accountNumber='N'
totalAmount='0.00'/>
<bill accountId='eb' customerName='OES-161' accountNumber='OE'
totalAmount='0.00'/>
5
eb


I want to store all the values in an array or variables, please help me
out in doing this


Thanks in advance
Vikash

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
NAKAMURA, Hiroshi
Guest
Posts: n/a
 
      06-08-2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

Vikash Kumar wrote:
> I have a xml file


Save this file to foo.xml and;

require 'xsd/mapping'
obj = XSD::Mapping.xml2obj(File.read("foo.xml"))
obj.xmlattr_billerName #=> "Vikash"
obj.bill.each do |bill|
p bill.xmlattr_accountId
end
#=> "1"
#=> "OAQ"
#=> "dOAQ"
#=> "ab"
#=> "eb"

An element can be accessed via the method which name is as same as the
element name. An attribute can be accessed via the method "xmlattr_" +
attribute name.

Regards,
// NaHi
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)

iQEVAwUBRmlWHh9L2jg5EEGlAQLCRAf/XdXTE2anttIuxQFQQaoWZAgeDC2Gw0MM
XvSwJMFibW8PAev+nJTuMExEV2v1oDbyNIn3KBK96Q0A5CZgMI UTwd7HTdM/Owsr
wRqyFlKcf8L34PsLTCwmQ4T0AlQuMyLiPi32I4lXodNu+htGWT RA3wbfd4Qcfg66
vPqQUI5kd9bOnRZ0CONbHKfN4bVbj6bZd3RFaSbfOEzVAgSovA xP/foWKvATOnfN
X64MdT11u5bmUUN30Xun/M+Y9RFZv6frk2LkkBQ5AcCy7dAN/j0uATc4f2kUdItZ
SxsfOKy4eYsPVNyLHvfNTXb4TWoPUZGyd6NK7BJ5aE45EA56ZM Co9A==
=mBbL
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
reading from a file, some times values in file may not be filled srikanth C Programming 1 06-28-2005 03:39 PM
Parsing an XML file for a particular element, reading the children, and getting database values Luis Esteban Valencia ASP .Net 0 06-20-2005 12:54 PM
XML schema validation of one xml block based on values from another xml block Andy XML 0 11-18-2004 11:04 PM



Advertisments