# Rexml: replace several text nodes by a single one

**URL:** <https://www.ruby-forum.com/t/rexml-replace-several-text-nodes-by-a-single-one/97601>\
**Category:** Ruby\
**Created:** [July 8, 2006, 6:35pm UTC](https://www.ruby-forum.com/t/rexml-replace-several-text-nodes-by-a-single-one/97601 "2006-07-08T18:35:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [July 8, 2006, 6:35pm UTC](https://www.ruby-forum.com/t/rexml-replace-several-text-nodes-by-a-single-one/97601/1 "2006-07-08T18:35:47Z")

</div>

Hi,

this sounds simple but I just don’t get it…

I want to replace the text child nodes of an REXML::Element by a new,  
single text node…

In order to do that, I want to remove any previous text nodes first

* * *

require ‘rexml/document’  
include REXML

doc = Document.new("")  
a = doc.elements[‘a’]  
a.delete\_element(‘b’)

# assert: a.texts.size == 2

# try #1

# 

# a.texts.clear

# –\> can’t modify frozen array (TypeError)

# try #2

# 

# iterate all elements, call delete\_element: fails, because

a.elements.size == 0

# MAGIC CODE GOES HERE!!!

a.text = “I am the only text in this element”

# assert a.texts.size == 1

* * *

I don’t think that setting all text nodes to zero-length string values  
is a _good_ solution.

thanks for your help!

regards  
peter

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [August 3, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/rexml-replace-several-text-nodes-by-a-single-one/97601/2 "2006-08-03T14:24:17Z")

</div>

I got something to work at least. This should give you an idea of how I  
did it:

```
def remove!
  case @node.node_type
  when :text
    @node.value = ''
  else
    @node.parent.send("delete_#{@node.node_type}", @node )
  end
end

def empty!
  each { |n| n.remove! }
end

```

T.

---

<div class="post-metadata">

**Author:** ![pete](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3ab097/32.png) [@pete](https://www.ruby-forum.com/u/pete)\
**Post date:** [July 8, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/rexml-replace-several-text-nodes-by-a-single-one/97601/3 "2006-07-08T19:27:30Z")

</div>

Pete wrote:

> Hi,
> 
> this sounds simple but I just don’t get it…
> 
> I want to replace the text child nodes of an REXML::Element by a new,  
> single text node…
> 
> In order to do that, I want to remove any previous text nodes first

OMG, I was just about to ask the same thing. I was trying to delete  
them all first, but I don;t even get how to do that:

node.parent.delete( node ) # soden;t seem to to work

Do I really have to do

case node  
when REXML::Element  
node.parent.delete\_element( node )  
when REXML::Attribute  
node.parent.delete\_element( node )  
when REXML::Text  
node.parent.delete\_text( node )  
else  
# else ?  
end

Tell me it ain’t so!

T.
