From: stefano Date: 2004-03-21T19:49:30+09:00 Subject: Re: REXML concatenation On Sat, 20 Mar 2004 00:12:25 +0900, Andrew Kulinich wrote: > I have some xml documents: > 1) > 2) > 3) > > I want to concatenate them to get > > > > > > > > > > > How can I make this using REXML? A bit clumsy, but it works (at least with the given example :-) require "rexml/document" class REXML::Element def same_as?(other) self.name == other.name and self.attribute("id") == other.attribute("id") end def merge(other) if other.name == self.name other.each_element do |child| _merge(child) end end end def _merge(other) if other.is_a?(REXML::Element) if matching_node = REXML::XPath.first(self, other.name) if matching_node.same_as?(other) matching_node.merge(other) else add_element(other) end else add_element(other) end end end end doc1 = REXML::Document.new("") doc2 = REXML::Document.new("") doc3 = REXML::Document.new("") doc = REXML::Document.new("") doc.merge(doc1) doc.merge(doc2) doc.merge(doc3) puts doc.to_s => -- ste