# REXML question

**URL:** <https://rubytalk.org/t/rexml-question/32456>\
**Category:** ruby-talk\
**Created:** [28 October 2006 23:19 UTC](https://rubytalk.org/t/rexml-question/32456 "2006-10-28T23:19:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pau\_Garcia\_i\_Quiles](https://avatars.discourse-cdn.com/v4/letter/p/b9e5f3/32.png) [@Pau\_Garcia\_i\_Quiles](https://rubytalk.org/u/Pau_Garcia_i_Quiles)\
**Post date:** [28 October 2006 23:19 UTC](https://rubytalk.org/t/rexml-question/32456/1 "2006-10-28T23:19:10Z")

</div>

Hello,

I am using REXML to create XML documents but I am facing a problem: I cannot  
add XML childs.

I want to create a parent element with REXML, then add a bunch of XML as a  
children (I already have that XML). I would like to do something like this:

xml = REXML::Element.new('parent')  
xml.add\_xml\_element('\<child\>\<attrib1\>value1\</attrib1\>\<attrib2\>value2\</attrib2\>\</child\>')

Then xml.to\_s would output:  
\<parent\>\<child\>\<attrib1\>value1\</attrib1\>\<attrib2\>value2\</attrib2\>\</child\>\</parent\>

I have tried with REXML::Element#add\_element, but it adds an extra '\<' before  
the child:  
\<parent\>\<\<child\>\<attrib1\>value1\</attrib1\>\<attrib2\>value2\</attrib2\>\</child\>\</parent\>

Am I trying something impossible?

Thank you.

> **···**
>
> --  
> Pau Garcia i Quiles  
> [http://www.elpauer.org](http://www.elpauer.org)  
> (Due to the amount of work, I usually need 10 days to answer)

---

<div class="post-metadata">

**Author:** ![Kalman\_Noel](https://avatars.discourse-cdn.com/v4/letter/k/ad7895/32.png) [@Kalman\_Noel](https://rubytalk.org/u/Kalman_Noel)\
**Post date:** [29 October 2006 08:20 UTC](https://rubytalk.org/t/rexml-question/32456/2 "2006-10-29T08:20:19Z")

</div>

Pau Garcia i Quiles:

> xml = REXML::Element.new('parent')  
> xml.add\_xml\_element('\<child\>\<attrib1\>value1\</attrib1\>\<attrib2\>value2\</attrib2\>\</child\>')
> 
> Then xml.to\_s would output:  
> \<parent\>\<child\>\<attrib1\>value1\</attrib1\>\<attrib2\>value2\</attrib2\>\</child\>\</parent\>

# »Element« for creating tags:  
parent = REXML::Element.new('parent')  
# »Document« for XML input:  
child = REXML::Document.new('\<child\>\<attrib1\>value1\</attrib1\>\</child\>')

parent \<\< child  
parent.to\_s

Kalman
