# Reading values from xml file

**URL:** <https://www.ruby-forum.com/t/reading-values-from-xml-file/106320>\
**Category:** Ruby\
**Created:** [June 8, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/reading-values-from-xml-file/106320 "2007-06-08T14:51:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikashkumar051](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@vikashkumar051](https://www.ruby-forum.com/u/vikashkumar051)\
**Post date:** [June 8, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/reading-values-from-xml-file/106320/1 "2007-06-08T14:51:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vikashkumar051](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/eb9ed0/32.png) [@vikashkumar051](https://www.ruby-forum.com/u/vikashkumar051)\
**Post date:** [June 8, 2007, 3:15pm UTC](https://www.ruby-forum.com/t/reading-values-from-xml-file/106320/2 "2007-06-08T15:15:28Z")

</div>

-----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-----
