REXML concatenation

I have some xml documents:

I want to concatenate them to get








How can I make this using REXML?

···


Andrew Kulinich
IT Group
Project manager
Phone/Fax +380 (372) 58-43-10
e-mail: Andrew.Kulinich@itgrp.net
http://www.itgrp.net

A bit clumsy, but it works (at least with the given example :slight_smile:

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

=>

···

On Sat, 20 Mar 2004 00:12:25 +0900, Andrew Kulinich wrote:

I have some xml documents:

I want to concatenate them to get








How can I make this using REXML?


ste