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
On Jan 29, 12:19 pm, hema gonaboina <[email protected]> wrote:
> Hi to everyone,
>
> I am getting xml in the below format
> <records>
> <grn>
> <first>1</first><second>2</second><third>3</third>
> </grn>
> <arn>
>
> <child1>First</child1><child2>Second</child2><child3>Third</child3>
> </arn>
> <arn>
>
> <child1>First</child1><child2>Second</child2><child3>Third</child3>
> </arn>
> <arn>
>
> <child1>First</child1><child2>Second</child2><child3>Third</child3>
> </arn>
> </records>
>
> 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---