ramamoorthy_villi
ramamoorthy_villi

Reputation: 2055

Push XML to another XML as child node using rails Nokogiri gem

Objective :

Push xml to another xml using Nokogiri rails gem

Here is Xml :

XML 1 :

<Hotellist>
  <hotel>
   <Name>Taj</Name>
   <location>New york,US</Name>   
  </hotel>
  <hotel>
   <Name>Hotel Oasaka</Name>
   <location>California,US</Name>   
  </hotel>
</Hotellist>

XML 2 :

 <hotel>
   <Name>Hotel Sherin</Name>
   <location>London,Uk</Name>   
  </hotel>

I want to push the xml2 value to xml1 as another child using Nokogiri or any other smarter way , any help ?

Upvotes: 0

Views: 183

Answers (1)

Tamer Shlash
Tamer Shlash

Reputation: 9523

xml1 = Nokogiri::XML("<Hotellist><hotel><Name>Taj</Name><location>New york,US</Name></hotel><hotel><Name>Hotel Oasaka</Name><location>California,US</Name></hotel></Hotellist>")
xml2 = Nokogiri::XML("<hotel><Name>Hotel Sherin</Name><location>London,Uk</Name></hotel>")

xml1.children.first.add_child(xml2.children.first.clone)

Should get the work done.

Upvotes: 1

Related Questions