# REXML: Raw tags in element text?

**URL:** <https://www.ruby-forum.com/t/rexml-raw-tags-in-element-text/121108>\
**Category:** Ruby\
**Created:** [November 23, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/rexml-raw-tags-in-element-text/121108 "2007-11-23T22:27:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 23, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/rexml-raw-tags-in-element-text/121108/1 "2007-11-23T22:27:22Z")

</div>

In REXML, is it possible to add raw tags to element text, without having  
to add elements for those tags?  
(I would like to add a few HTML tags like to strings, before I  
know what element nodes they will appear at.)

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 24, 2007, 2:55am UTC](https://www.ruby-forum.com/t/rexml-raw-tags-in-element-text/121108/2 "2007-11-24T02:55:50Z")

</div>

On Nov 23, 2:26 pm, Jari W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In REXML, is it possible to add raw tags to element text, without having  
> to add elements for those tags?  
> (I would like to add a few HTML tags like to strings, before I  
> know what element nodes they will appear at.)

Are you wanting to ad XML elements as children of another without  
using the DOM to create them (like setting .innerHTML in a web  
browser, causing it to parse your string and create elements as  
necessary),

or are you wanting to set the contents of a text node to have  
characters that look like XML tags, but have it automatically escape  
them for you?

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 24, 2007, 7:48am UTC](https://www.ruby-forum.com/t/rexml-raw-tags-in-element-text/121108/3 "2007-11-24T07:48:04Z")

</div>

Phrogz wrote:

> necessary),  
> Yes, this is what I want.

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 24, 2007, 9:51am UTC](https://www.ruby-forum.com/t/rexml-raw-tags-in-element-text/121108/4 "2007-11-24T09:51:56Z")

</div>

2007/11/24, Jari W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > browser, causing it to parse your string and create elements as  
> > necessary),
> 
> Yes, this is what I want.

I believe you can do it (i.e. add raw tags to text) BUT they will not  
show up as tags when output. Which is completely logical once you  
think about it. If you work with a DOM you have to add nodes as  
nodes. Maybe you can just create a small sub tree with a **node and  
your text as nested text and later put that node into the tree.**

 

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 24, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/rexml-raw-tags-in-element-text/121108/5 "2007-11-24T16:30:17Z")

</div>

Robert K. wrote:

> > > browser, causing it to parse your string and create elements as  
> > > necessary),  
> > > Yes, this is what I want.
> 
> I believe you can do it (i.e. add raw tags to text) BUT they will not  
> show up as tags when output. Which is completely logical once you  
> think about it. If you work with a DOM you have to add nodes as  
> nodes. Maybe you can just create a small sub tree with a **node and  
> your text as nested text and later put that node into the tree.**

Since nothing seems to exist I wrote my own 2-line method: It adds a raw  
string, parses it and passes all nodes to the parent element. Here’s the  
code if anyone else would need it:

* * *

## class REXML::Element def add\_raw\_text(raw\_text) doc = REXML::Document.new(""+raw\_text+"") doc.root.children.each { |e| self \<\< e } end end

Example:  
myelement.add\_raw\_text(“here’s a **bold** test”)

Best regards,

Jari W.
