# REXML element reading \<br /\> error

**URL:** <https://www.ruby-forum.com/t/rexml-element-reading-br-error/113505>\
**Category:** Ruby\
**Created:** [August 31, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/rexml-element-reading-br-error/113505 "2007-08-31T21:57:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 31, 2007, 9:57pm UTC](https://www.ruby-forum.com/t/rexml-element-reading-br-error/113505/1 "2007-08-31T21:57:54Z")

</div>

When reading in the site element from my xml file using rexml it seems  
to be chopping the rest of the text off after the first

The value in the XML file is below  
123 street  
amstown  
amserland

element = REXML::XPath.first(doc, ‘//Site’)

puts element.text #shows 123 Street

How can i get the full data and once i have it i can remove the   
  
I cant find any information on this???

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [August 31, 2007, 10:19pm UTC](https://www.ruby-forum.com/t/rexml-element-reading-br-error/113505/2 "2007-08-31T22:19:36Z")

</div>

On 8/31/07, John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When reading in the site element from my xml file using rexml it seems  
> to be chopping the rest of the text off after the first
> 
> The value in the XML file is below  
> 123 street  
> amstown  
> amserland
> 
> element = REXML::XPath.first(doc, ‘//Site’)

I’d suggest using a bit more XPath, both text() and a each {} to  
iterate through the text nodes (which are distinct):

$ irb -r rexml/document --prompt xmp  
a = REXML::Document.new(“123  
street  
amstown  
amserland”)

# =\> … \</\>

REXML::XPath.first(a, ‘//Site’).text

# =\> “123 street”

REXML::XPath.first(a, ‘//Site/text()’).to\_s

# =\> “123 street”

REXML::XPath.each(a, ‘//Site/text()’) {|el| puts el}  
123 street  
amstown  
amserland

# =\> [“123 street”, “amstown”, “amserland”]

HTH,  
Keith

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [September 1, 2007, 1:27am UTC](https://www.ruby-forum.com/t/rexml-element-reading-br-error/113505/3 "2007-09-01T01:27:51Z")

</div>

Hi,

At Sat, 1 Sep 2007 05:18:48 +0900,  
Keith F. wrote in [ruby-talk:266990]:

> I’d suggest using a bit more XPath, both text() and a each {} to  
> iterate through the text nodes (which are distinct):
> 
> $ irb -r rexml/document --prompt xmp  
> a = REXML::Document.new(“123 street  
> amstown  
> amserland”)
> 
> # =\> … \</\>
> 
> REXML::XPath.first(a, ‘//Site’).text
> 
> # =\> “123 street”

Seems like that just REXML::XPath.first(a, ‘//Site’).to\_s  
returns the whole content.

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [September 1, 2007, 1:50am UTC](https://www.ruby-forum.com/t/rexml-element-reading-br-error/113505/4 "2007-09-01T01:50:52Z")

</div>

Keith F. wrote:

> REXML::XPath.each(a, ‘//Site/text()’) {|el| puts el}

The assert\_xpath plugin wraps that up in this convenient method:

```
class REXML::Element
  def inner_text
    return self.each_element( './/text()' ){}.join( '' )
  end
end

```

…  
def test\_absolve\_breaks  
a = REXML::Document.new(“123  
street  
amstown  
amserland”)  
assert\_equal “123 streetamstownamserland”, a.inner\_text  
end

Come to think of it, that’s not terribly programmer-friendly! Let’s  
upgrade  
it a little…

```
  assert_equal "123 streetamstownamserland", a.inner_text
  assert_equal "123 street\namstown\namserland", a.inner_text("\n")

```

…  
def inner\_text(interstitial = ‘’)  
return self.each\_element( ‘.//text()’ ){}.join(interstitial)  
end

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [September 1, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/rexml-element-reading-br-error/113505/5 "2007-09-01T19:21:33Z")

</div>

On Sat, 01 Sep 2007 04:57:57 +0900, John B. wrote:

> When reading in the site element from my xml file using rexml it seems  
> to be chopping the rest of the text off after the first

Not quite. It gives you the _first_ text element.

> The value in the XML file is below  
> 123 street  
> amstown  
> amserland
> 
> element = REXML::XPath.first(doc, ‘//Site’)
> 
> puts element.text #shows 123 Street
> 
> How can i get the full data and once i have it i can remove the   
> I  
> cant find any information on this???

You can’t find any specific info because there isn’t anything specific.  
You have an XML element that contains a text node, an empty element  
named  
br, another text node, another empty element named br and another text  
node. In the XML world,   
 is a node like any other.

The REXML::Element.texts method is what you are looking for:

$ irb  
irb(main):001:0\> require “rexml/document”  
=\> true

irb(main):002:0\> doc=REXML::Document.new("123  
street  
amstown\<br/

> amserland")  
> =\> … \</\>

irb(main):003:0\> doc.root.texts  
=\> [“123 street”, “amstown”, “amserland”]

irb(main):004:0\> doc.root.texts.join " "  
=\> “123 street amstown amserland”

Enjoy!
