Reading values from xml file

V

Vikash Kumar

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="";totalDebits="";
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
 
N

NAKAMURA, Hiroshi

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

Vikash said:
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+nJTuMExEV2v1oDbyNIn3KBK96Q0A5CZgMIUTwd7HTdM/Owsr
wRqyFlKcf8L34PsLTCwmQ4T0AlQuMyLiPi32I4lXodNu+htGWTRA3wbfd4Qcfg66
vPqQUI5kd9bOnRZ0CONbHKfN4bVbj6bZd3RFaSbfOEzVAgSovAxP/foWKvATOnfN
X64MdT11u5bmUUN30Xun/M+Y9RFZv6frk2LkkBQ5AcCy7dAN/j0uATc4f2kUdItZ
SxsfOKy4eYsPVNyLHvfNTXb4TWoPUZGyd6NK7BJ5aE45EA56ZMCo9A==
=mBbL
-----END PGP SIGNATURE-----
 

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top