# Merge XML documents with Ruby

**URL:** <https://www.ruby-forum.com/t/merge-xml-documents-with-ruby/149620>\
**Category:** Ruby\
**Created:** [September 26, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/merge-xml-documents-with-ruby/149620 "2008-09-26T20:05:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![harm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b4bc9f/32.png) [@harm](https://www.ruby-forum.com/u/harm)\
**Post date:** [September 26, 2008, 8:05pm UTC](https://www.ruby-forum.com/t/merge-xml-documents-with-ruby/149620/1 "2008-09-26T20:05:45Z")

</div>

Dear group,

Currently I’m working with Ruby to merge several XML documents into  
one. I’m using Builder to construct the ‘master’ XML file:

```
x = Builder::XmlMarkup.new(:indent => 2)
x.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
result = x.header do |xml|
  some_hash.each do |key, values|
    xml.company(key)
    values.each do |value|
      xml.department(value.to_xml)
    end
  end
end

```

However on the spot where I write ‘xml.department’ trouble strikes.  
The ‘value’ already responds to the to\_xml method and generates  
perfectly fine XML. I’d like to insert that XML into the master file.  
If I do it the way displayed above the department tag appears twice  
(once from the xml.department call and once it is used as the header  
tag of the department.to\_xml result). Moreover the instruct tag is  
inserted and all \< and \> characters are escaped.

Is there anyway of doing this gracefully?  
Any help is greatly appreciated.

With kind regards,  
Harm

---

<div class="post-metadata">

**Author:** ![harm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b4bc9f/32.png) [@harm](https://www.ruby-forum.com/u/harm)\
**Post date:** [September 26, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/merge-xml-documents-with-ruby/149620/2 "2008-09-26T21:55:29Z")

</div>

On Sep 26, 2:04 pm, Harm [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> values.each do |value|
> xml.department(value.to_xml)
> end
> end
> end
> 
> ```
> 
> However on the spot where I write ‘xml.department’ trouble strikes.  
> The ‘value’ already responds to the to\_xml method and generates  
> perfectly fine XML.

You didn’t give us much to go on. What objects does the some\_hash  
contain? Do you have sample output?

– Mark.

---

<div class="post-metadata">

**Author:** ![harm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b4bc9f/32.png) [@harm](https://www.ruby-forum.com/u/harm)\
**Post date:** [September 26, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/merge-xml-documents-with-ruby/149620/3 "2008-09-26T22:15:24Z")

</div>

On Sep 26, 9:51 pm, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > x.instruct! :xml, :version => "1.0", :encoding => "UTF-8"
> > 
> > ```
> > 
> > The ‘value’ already responds to the to\_xml method and generates  
> > perfectly fine XML.
> 
> You didn’t give us much to go on. What objects does the some\_hash  
> contain? Do you have sample output?
> 
> – Mark.

Have you try the hpricot ([http://code.whytheluckystiff.net/hpricot/](http://code.whytheluckystiff.net/hpricot/)) ?

Generate your xml snippets and then merge them in one final valid xml.

Nevertheless, it would be useful to have more details.
