Reading values from xml file

I have a xml file

<?xml version="1.0" encoding="UTF-8"?>

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





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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs