# Help please with REXML

**URL:** <https://www.ruby-forum.com/t/help-please-with-rexml/191687>\
**Category:** Ruby\
**Created:** [July 16, 2010, 7:23am UTC](https://www.ruby-forum.com/t/help-please-with-rexml/191687 "2010-07-16T07:23:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [July 16, 2010, 7:23am UTC](https://www.ruby-forum.com/t/help-please-with-rexml/191687/1 "2010-07-16T07:23:53Z")

</div>

hello,

I am trying to make a simple ruby script that can look at a set of xml  
files, and make a set of folders based on the content of the XML files.  
As it stands I can import the data with REXML, but when I run the script  
I get a set of errors(I posted below the code) talking about TypeError.

Take a look below and let me know what you think…  
**CODE** \_  
#!/usr/bin/ruby

require ‘rexml/document’  
include REXML

jnumber\_file = File.new(“XML\_EXPORT/export\_j\_number.xml”)  
jname\_file = File.new(“XML\_EXPORT/export\_j\_name.xml”)  
cnumber\_file = File.new(“XML\_EXPORT/export\_cnumber.xml”)  
cname\_file = File.new(“XML\_EXPORT/export\_c\_name.xml”)

jnumber\_file = Document.new(jnumber\_file)  
jname\_file = Document.new(jname\_file)  
cnumber\_file = Document.new(cnumber\_file)  
cname\_file = Document.new(cname\_file)

puts jnumber\_file  
puts jname\_file  
puts cnumbe\_file  
puts cname\_file

class Job  
attr\_accessor :client\_num, :client\_name, :job\_num, :job\_name;

def generate\_files()  
# print “Client Num is " + @client\_num.to\_s + “\n”  
client\_dir = @client\_num.to\_s + “_" + @client\_name  
job\_dir = @job\_num.to\_s + "_” + @job\_name  
if(! File.exist?(client\_dir))  
Dir.mkdir(client\_dir);  
end  
Dir.mkdir(client\_dir+”/"+job\_dir);

```
Dir.mkdir(client_dir+"/"+job_dir+"/parent");
Dir.mkdir(client_dir+"/"+job_dir+"/parent/old");
Dir.mkdir(client_dir+"/"+job_dir+"/parent/current");

Dir.mkdir(client_dir+"/"+job_dir+"/children");
Dir.mkdir(client_dir+"/"+job_dir+"/children/old");
Dir.mkdir(client_dir+"/"+job_dir+"/children/current");

```

end  
end

j = Job.new  
j.client\_name = cname  
j.client\_num = cnumberfile  
j.job\_name = cname  
j.job\_num = jname\_file  
j.generate\_files()  
\_\_\__ **END CODE** _  
**ERROE**

director\_v3.rb:28:in `+': can't convert REXML::Document into String (TypeError) from director_v4.rb:28:in`generate\_files’  
from director\_v4.rb:50  
\_\_\_\_ **END ERROR**

Anything yall can see to help would be awsome!

Thanks

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [July 16, 2010, 8:17am UTC](https://www.ruby-forum.com/t/help-please-with-rexml/191687/2 "2010-07-16T08:17:04Z")

</div>

If you can afford the time to install Nokogiri, this might be a good  
direction to take.  
It has better mindshare at the moment.  
Anyways, could you re-run with the formatting below?  
(One typo found).

MarkT

#=================================  
require ‘rexml/document’  
include REXML  
class Job  
attr\_accessor :client\_num, :client\_name, :job\_num, :job\_name

def generate\_files()  
client\_dir = @client\_num.to\_s + “_" + @client\_name  
job\_dir = @job\_num.to\_s + "_” + @job\_name  
unless File.exist?(client\_dir) then Dir.mkdir(client\_dir) end  
Dir.mkdir(client\_dir+"/"+job\_dir)  
Dir.mkdir(client\_dir+"/"+job\_dir+"/parent")  
Dir.mkdir(client\_dir+"/"+job\_dir+"/parent/old")  
Dir.mkdir(client\_dir+"/"+job\_dir+"/parent/current")  
Dir.mkdir(client\_dir+"/"+job\_dir+"/children")  
Dir.mkdir(client\_dir+"/"+job\_dir+"/children/old")  
Dir.mkdir(client\_dir+"/"+job\_dir+"/children/current") end end

def declareFiles  
jnumber\_file = File.new(“XML\_EXPORT/export\_j\_number.xml”)  
jname\_file = File.new(“XML\_EXPORT/export\_j\_name.xml”)  
cnumber\_file = File.new(“XML\_EXPORT/export\_cnumber.xml”)  
cname\_file = File.new(“XML\_EXPORT/export\_c\_name.xml”)  
jnumber\_file = Document.new(jnumber\_file)  
jname\_file = Document.new(jname\_file)  
cnumber\_file = Document.new(cnumber\_file)  
cname\_file = Document.new(cname\_file) end

j = Job.new  
j.client\_name = cname  
j.client\_num = cnumberfile  
j.job\_name = cname  
j.job\_num = jname\_file  
j.declareFiles  
puts jnumber\_file, jname\_file, cnumber\_file, cname\_file  
j.generate\_files()  
#=================================

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [July 16, 2010, 9:22am UTC](https://www.ruby-forum.com/t/help-please-with-rexml/191687/3 "2010-07-16T09:22:32Z")

</div>

Thank you for you help!  
Sadly this didn’t end up working I got errors (posted below)

**ERROR** \_\_\_\_\_\_\_\_  
undefined local variable or method `cnumber\_file’ for main:Object  
(NameError)  
**END ERROR** \_\_\_\_

again sorry for the newness and thanks!!! fro your help

> #=================================  
> require ‘rexml/document’  
> include REXML  
> class Job  
> attr\_accessor :client\_num, :client\_name, :job\_num, :job\_name
> 
> def generate\_files()  
> client\_dir = @client\_num.to\_s + “_" + @client\_name  
> job\_dir = @job\_num.to\_s + "_” + @job\_name  
> unless File.exist?(client\_dir) then Dir.mkdir(client\_dir) end  
> Dir.mkdir(client\_dir+"/"+job\_dir)  
> Dir.mkdir(client\_dir+"/"+job\_dir+"/parent")  
> Dir.mkdir(client\_dir+"/"+job\_dir+"/parent/old")  
> Dir.mkdir(client\_dir+"/"+job\_dir+"/parent/current")  
> Dir.mkdir(client\_dir+"/"+job\_dir+"/children")  
> Dir.mkdir(client\_dir+"/"+job\_dir+"/children/old")  
> Dir.mkdir(client\_dir+"/"+job\_dir+"/children/current") end end
> 
> def declareFiles  
> jnumber\_file = File.new(“XML\_EXPORT/export\_j\_number.xml”)  
> jname\_file = File.new(“XML\_EXPORT/export\_j\_name.xml”)  
> cnumber\_file = File.new(“XML\_EXPORT/export\_cnumber.xml”)  
> cname\_file = File.new(“XML\_EXPORT/export\_c\_name.xml”)  
> jnumber\_file = Document.new(jnumber\_file)  
> jname\_file = Document.new(jname\_file)  
> cnumber\_file = Document.new(cnumber\_file)  
> cname\_file = Document.new(cname\_file) end
> 
> j = Job.new  
> j.client\_name = cname  
> j.client\_num = cnumberfile  
> j.job\_name = cname  
> j.job\_num = jname\_file  
> j.declareFiles  
> puts jnumber\_file, jname\_file, cnumber\_file, cname\_file  
> j.generate\_files()  
> #=================================

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [July 16, 2010, 4:03pm UTC](https://www.ruby-forum.com/t/help-please-with-rexml/191687/4 "2010-07-16T16:03:45Z")

</div>

On Fri, Jul 16, 2010 at 1:23 AM, Ben M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hello,
> 
> I am trying to make a simple ruby script that can look at a set of xml  
> files, and make a set of folders based on the content of the XML files.  
> As it stands I can import the data with REXML, but when I run the script  
> I get a set of errors(I posted below the code) talking about TypeError.
> 
> Take a look below and let me know what you think…

$ ruby /tmp/code\_below  
/tmp/code\_below:6:in `initialize': No such file or directory - XML_EXPORT/export_j_number.xml (Errno::ENOENT) from /tmp/code_below:6:in `new’  
from /tmp/code\_below:6

Beyond that, is the posted code the same code that produced your sample  
output?

44 j.client\_name = cname  
46 j.job\_name = cname

But, cname is never set to anything before this point (or after).

9 cname\_file = File.new(“XML\_EXPORT/export\_c\_name.xml”)  
14 cname\_file = Document.new(cname\_file)  
19 puts cname\_file

But, cname\_file is never used after this point.

18 puts cnumbe\_file

There is no cnumbe\_file.

26 client\_dir = @client\_num.to\_s + “\_” + @client\_name

@client\_num comes from:

45 j.client\_num = cnumberfile  
13 cnumber\_file = Document.new(cnumber\_file)

@client\_name comes from:

44 j.client\_name = cname

Should that have been?:

44 j.client\_name = cname\_file  
14 cname\_file = Document.new(cname\_file)

So, in line 26 we have something like:

26 client\_dir = REXML::Document.to\_s + “_" + REXML::Document  
26 client\_dir = + "_” + REXML::Document  
26 client\_dir = + REXML::Document  
TypeError: can’t convert REXML::Document into String
