# Taking out text between symbols and joining together

**URL:** <https://www.ruby-forum.com/t/taking-out-text-between-symbols-and-joining-together/190886>\
**Category:** Ruby\
**Created:** [June 30, 2010, 1:54am UTC](https://www.ruby-forum.com/t/taking-out-text-between-symbols-and-joining-together/190886 "2010-06-30T01:54:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob05](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e274bd/32.png) [@bob05](https://www.ruby-forum.com/u/bob05)\
**Post date:** [June 30, 2010, 1:54am UTC](https://www.ruby-forum.com/t/taking-out-text-between-symbols-and-joining-together/190886/1 "2010-06-30T01:54:18Z")

</div>

Hello,

I have some text files that I would like to extract text from, then join  
them on one single line and save them to a text file.

Here is an example of the text I want to take out:

Protein complexes in Saccharomyces cerevisiae (GPM06600002310) GPM06600002310 None

Here is how I would like the text to save as:

Protein complexes in Saccharomyces cerevisiae  
(GPM06600002310)GPM06600002310 None

So far I have this:

require ‘rexml/document’  
include REXML  
file = File.new(“1.xml”)  
doc = Document.new(file)  
puts doc  
aFile = File.new(“1.txt”, “w”)  
aFile.write(doc)  
aFile.close

I was wondering, how can you split out text and join them on one line?

---

<div class="post-metadata">

**Author:** ![bob05](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e274bd/32.png) [@bob05](https://www.ruby-forum.com/u/bob05)\
**Post date:** [June 30, 2010, 8:21am UTC](https://www.ruby-forum.com/t/taking-out-text-between-symbols-and-joining-together/190886/2 "2010-06-30T08:21:40Z")

</div>

On Wed, Jun 30, 2010 at 1:54 AM, Bob05 Dr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> None  
> include REXML  
> file = File.new(“1.xml”)  
> doc = Document.new(file)  
> puts doc  
> aFile = File.new(“1.txt”, “w”)  
> aFile.write(doc)  
> aFile.close
> 
> I was wondering, how can you split out text and join them on one line?

First of all, your document doesn’t parse well, because it has two root  
nodes.  
After solving that, what you need is to get to each element and  
extract its text children nodes.  
Take a look at:

[http://www.germane-software.com/software/rexml/docs/tutorial.html](http://www.germane-software.com/software/rexml/docs/tutorial.html)

And the methods:

elements  
[]  
text

of the API. Experiment a little in IRB:

irb(main):001:0\> s = \<\<EOF  
irb(main):002:0" Protein complexes in Saccharomyces cerevisiae  
irb(main):003:0" (GPM06600002310)  
irb(main):004:0" GPM06600002310  
irb(main):005:0" None  
irb(main):006:0" EOF  
=\> “Protein complexes in Saccharomyces  
cerevisiae\n(GPM06600002310)\nGPM06600002310\nNone\n”  
irb(main):007:0\>  
irb(main):008:0\*  
irb(main):009:0\* require ‘rexml/document’  
=\> true  
irb(main):010:0\> include REXML  
=\> Object  
irb(main):011:0\> doc = Document.new s  
REXML::ParseException: #\<RuntimeError: attempted adding second root  
element to document\>

ooooops, two root elements. I’ll add a fake one surrounding everything:

irb(main):012:0\> s = \<\<EOF  
irb(main):013:0"   
irb(main):014:0" Protein complexes in Saccharomyces cerevisiae  
irb(main):015:0" (GPM06600002310)  
irb(main):016:0" GPM06600002310  
irb(main):017:0" None  
irb(main):018:0"   
irb(main):019:0" EOF  
=\> “\nProtein complexes in Saccharomyces  
cerevisiae\n(GPM06600002310)\nGPM06600002310\nNone\n\n”  
irb(main):020:0\> doc = Document.new s  
=\> … \</\>  
irb(main):025:0\> doc.elements  
=\> #\<REXML::Elements:0xb72907e0 @element= … \</\>\>  
irb(main):026:0\> doc.elements.each {|el| p el}  
 … \</\>  
=\> [… \</\>]  
irb(main):027:0\> doc.to\_a  
=\> [… \</\>, “\n”]  
irb(main):028:0\> doc.elements.to\_a  
=\> [… \</\>]  
irb(main):032:0\> doc.elements[“/Title”]  
=\> nil  
irb(main):033:0\> doc.elements[“Title”]  
=\> nil  
irb(main):034:0\> root = doc.root  
=\> … \</\>  
irb(main):035:0\> root.elements[“Title”]  
=\> … \</\>  
irb(main):036:0\> root.elements[“Title”].to\_s  
=\> “Protein complexes in Saccharomyces  
cerevisiae\n(GPM06600002310)”

Look, it seems that with that I can get the text of the Title element.  
Let’s see if there’s a better way:

irb(main):039:0\> root.elements[“Title”].methods.sort  
=\> [“\<\<”, “==”, “===”, “=~”, “[]”, “[]=”, “ **id** ”, “ **send** ”, “add”,  
“add\_attribute”, “add\_attributes”, “add\_element”, “add\_namespace”,  
“add\_text”, “all?”, “any?”, “attribute”, “attributes”, “bytes”,  
“cdatas”, “children”, “class”, “clone”, “collect”, “comments”,  
“context”, “context=”, “count”, “cycle”, “dclone”, “deep\_clone”,  
“delete”, “delete\_at”, “delete\_attribute”, “delete\_element”,  
“delete\_if”, “delete\_namespace”, “detect”, “display”, “document”,  
“drop”, “drop\_while”, “dup”, “each”, “each\_child”, “each\_cons”,  
“each\_element”, “each\_element\_with\_attribute”,  
“each\_element\_with\_text”, “each\_index”, “each\_recursive”,  
“each\_slice”, “each\_with\_index”, “elements”, “entries”, “enum\_cons”,  
“enum\_for”, “enum\_slice”, “enum\_with\_index”, “eql?”, “equal?”,  
“expanded\_name”, “extend”, “find”, “find\_all”, “find\_first\_recursive”,  
“find\_index”, “first”, “freeze”, “frozen?”, “fully\_expanded\_name”,  
“get\_elements”, “get\_text”, “grep”, “group\_by”, “has\_attributes?”,  
“has\_elements?”, “has\_name?”, “has\_text?”, “hash”, “id”,  
“ignore\_whitespace\_nodes”, “include?”, “indent”, “index”,  
“index\_in\_parent”, “inject”, “insert\_after”, “insert\_before”,  
“inspect”, “instance\_eval”, “instance\_exec”, “instance\_of?”,  
“instance\_variable\_defined?”, “instance\_variable\_get”,  
“instance\_variable\_set”, “instance\_variables”, “instructions”,  
“is\_a?”, “kind\_of?”, “length”, “local\_name”, “map”, “max”, “max\_by”,  
“member?”, “method”, “methods”, “min”, “min\_by”, “minmax”,  
“minmax\_by”, “name”, “name=”, “namespace”, “namespaces”,  
“next\_element”, “next\_sibling”, “next\_sibling=”, “next\_sibling\_node”,  
“nil?”, “node\_type”, “none?”, “object\_id”, “one?”, “parent”,  
“parent=”, “parent?”, “partition”, “prefix”, “prefix=”, “prefixes”,  
“previous\_element”, “previous\_sibling”, “previous\_sibling=”,  
“previous\_sibling\_node”, “private\_methods”, “protected\_methods”,  
“public\_methods”, “push”, “raw”, “reduce”, “reject”, “remove”,  
“replace\_child”, “replace\_with”, “respond\_to?”, “reverse\_each”,  
“root”, “root\_node”, “select”, “send”, “singleton\_methods”, “size”,  
“sort”, “sort\_by”, “taint”, “tainted?”, “take”, “take\_while”, “tap”,  
“text”, “text=”, “texts”, “to\_a”, “to\_enum”, “to\_s”, “to\_set”, “type”,  
“unshift”, “untaint”, “whitespace”, “write”, “xpath”, “zip”]

There’s a text method in there, would that do what I expect?

irb(main):040:0\> root.elements[“Title”].text  
=\> “Protein complexes in Saccharomyces cerevisiae\n(GPM06600002310)”

Bingo !

Is there a way to access it directly from the doc, instead of having a  
root variable?

irb(main):042:0\> doc.elements[“ROOT/Title”].text  
=\> “Protein complexes in Saccharomyces cerevisiae\n(GPM06600002310)”

Now you can do the same for the other elements. I also recommend you  
learn XPath and CSS selectors if you are going to be parsing markup,  
and also look at other parsers like Nokogiri. This example was pretty  
simple, but these things can get nasty.

Jesus.
