# How to access a xml thourgh Ruby on rails

**URL:** <https://www.ruby-forum.com/t/how-to-access-a-xml-thourgh-ruby-on-rails/158919>\
**Category:** Rails\
**Created:** [January 29, 2009, 1:09pm UTC](https://www.ruby-forum.com/t/how-to-access-a-xml-thourgh-ruby-on-rails/158919 "2009-01-29T13:09:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hema\_mca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hema\_mca](https://www.ruby-forum.com/u/hema_mca)\
**Post date:** [January 29, 2009, 1:09pm UTC](https://www.ruby-forum.com/t/how-to-access-a-xml-thourgh-ruby-on-rails/158919/1 "2009-01-29T13:09:31Z")

</div>

Hi to everyone,

```
   I am getting xml in the below format

```

 123 

FirstSecondThird

FirstSecondThird

FirstSecondThird

I have to store this xml in my table sample which contain the fields as  
id,first,second,third,child1,child2 and  
child3,all are string fields except id field

and my record structure to be

## id first second third child1 child2 child3

## 1 1 2 3 First Second Third 2 1 2 3 First Second Third 3 1 2 3 First Second Third

Can any One tell me how to acheive this

I tried with XMLSimple,Rexml or even with Hashs

With XMLSimple Some garbage getting inserted into the table along with  
the  
value  
through hashes I can access each element and put in that table object  
but I’m getting Hashes which includes array also So for 3 entries we  
can do  
but it is large process for more records (accessing each element). And  
not a  
feasible solution also

Please help me how can I acheive these

---

<div class="post-metadata">

**Author:** ![hema\_mca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hema\_mca](https://www.ruby-forum.com/u/hema_mca)\
**Post date:** [January 29, 2009, 2:00pm UTC](https://www.ruby-forum.com/t/how-to-access-a-xml-thourgh-ruby-on-rails/158919/2 "2009-01-29T14:00:54Z")

</div>

Have you tried Nokogiri? You can access elements through XPath and  
iterate over them easily:

require ‘nokogiri’

xml\_doc = Nokogiri::XML(@string\_with\_your\_xml)  
grn\_elements = xml\_doc.xpath(’//grn’)  
arn\_elements = xml\_doc.xpath(’//arn’)  
arn\_elements[0].children each do |child|  
puts “Name: #{child.name} Value: #{child.inner\_text}”  
end

---

<div class="post-metadata">

**Author:** ![hema\_mca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/779978/32.png) [@hema\_mca](https://www.ruby-forum.com/u/hema_mca)\
**Post date:** [February 4, 2009, 12:13pm UTC](https://www.ruby-forum.com/t/how-to-access-a-xml-thourgh-ruby-on-rails/158919/3 "2009-02-04T12:13:06Z")

</div>

Thank you

But it is trowing the error as

NoMethodError: undefined method `each’ for #\<Nokogiri::XML::Element:  
0x45146a0\>

when i try with each

is there any way to access the arn\_elements[0].children ?

On Jan 29, 5:59 pm, Bosko I. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
