# XML Generator/Merger

**URL:** <https://www.ruby-forum.com/t/xml-generator-merger/189203>\
**Category:** Ruby\
**Created:** [May 26, 2010, 6:45pm UTC](https://www.ruby-forum.com/t/xml-generator-merger/189203 "2010-05-26T18:45:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fuglyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9dc877/32.png) [@fuglyducky](https://www.ruby-forum.com/u/fuglyducky)\
**Post date:** [May 26, 2010, 6:45pm UTC](https://www.ruby-forum.com/t/xml-generator-merger/189203/1 "2010-05-26T18:45:50Z")

</div>

Before I get into this, I need to say that I am not a programmer and  
have very limited programming experience in any language, let alone  
Ruby. With that said, Ruby is the language I know the best (which  
really isn’t saying much). Regardless…

I have been working on a script that will take multiple predefined XML  
template files and merge them as needed. I was able to do this by:

1. Convert file1 XML into an array where each line is an element in  
the array
2. Convert entire file2 into a string and insert it into the second to  
last element in file1 array  
(this is to insert a subsection of XML inside the tag of the  
file1 XML

Example:

file1:

file2:

Merged:

This works perfectly. My problem is that I need to randomize the  
number of individual pieces like this…

-single Header  
-multiple subElem1 (1+)  
-each subElem1 can contain multiple subElem2 (1+)  
-each subElem2 may contain multiple subElem3 and subElem4 (1+)

Keep in mind that each section has a closing tag so I need a way to  
merge or insert and simply append. My thought was to create a  
multidimensional array and merge from the bottom up but I was  
wondering if there is a faster and/or cleaner way of doing this? It  
doesn’t have to be pretty or fast.

Does anyone have any idea on how I should/could proceed?

Thanks!!!

---

<div class="post-metadata">

**Author:** ![fuglyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9dc877/32.png) [@fuglyducky](https://www.ruby-forum.com/u/fuglyducky)\
**Post date:** [May 26, 2010, 6:50pm UTC](https://www.ruby-forum.com/t/xml-generator-merger/189203/2 "2010-05-26T18:50:31Z")

</div>

On May 26, 9:42 am, fuglyducky [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. Convert entire file2 into a string and insert it into the second to
> 
> Keep in mind that each section has a closing tag so I need a way to  
> merge or insert and simply append. My thought was to create a  
> multidimensional array and merge from the bottom up but I was  
> wondering if there is a faster and/or cleaner way of doing this? It  
> doesn’t have to be pretty or fast.
> 
> Does anyone have any idea on how I should/could proceed?
> 
> Thanks!!!

Sorry…I was typing too fast…

Keep in mind that each section has a closing tag so I need a way to  
merge or insert and NOT simply append.

---

<div class="post-metadata">

**Author:** ![fuglyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9dc877/32.png) [@fuglyducky](https://www.ruby-forum.com/u/fuglyducky)\
**Post date:** [May 26, 2010, 9:15pm UTC](https://www.ruby-forum.com/t/xml-generator-merger/189203/3 "2010-05-26T21:15:42Z")

</div>

On May 26, 9:47 am, fuglyducky [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > template files and merge them as needed. I was able to do this by:  
> > file1:
> > 
> > -multiple subElem1 (1+)
> 
> > Thanks!!!
> 
> Sorry…I was typing too fast…
> 
> Keep in mind that each section has a closing tag so I need a way to  
> merge or insert and NOT simply append.

Forget it…I got it. Nested for loops do the trick.
