# REXML usage

**URL:** <https://www.ruby-forum.com/t/rexml-usage/79492>\
**Category:** Ruby\
**Created:** [December 15, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/rexml-usage/79492 "2006-12-15T17:10:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [December 15, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/rexml-usage/79492/1 "2006-12-15T17:10:40Z")

</div>

I’m developing a project that allows my client to import an XML file.  
The XML is from Excel, generated by performing a “Save-As” operation to  
save to XML. When I used a small (1kb) hand-generated XML file, the  
parsing worked fine. When I try to import the large (426kb) file, I’m  
getting a nil root object. Here’s some of my code:

# it seems as if the file is larger than some size, it will be passed to

us as a File object, not a

# string. If the file is small, the string contents will be passed.

def parse\_import\_file(file\_obj)  
if file\_obj.kind\_of? String  
@source = file\_obj  
elsif file\_obj.kind\_of? File  
@source = IOSource.new(file\_obj)  
end

```
@xml_document = REXML::Document.new(@source)
logger.info("XML version is '#{@xml_document.version}'")
logger.info("XML encoding is '#{@xml_document.encoding()}'")
logger.info("XML doctype is '#{@xml_document.doctype}'")

doc_root = @xml_document.root
if doc_root != nil @xml_document.root?
  #@xml_document.elements.each('ss:Workbook/ss:Worksheet') do

```

|worksheet\_element|  
@xml\_document.each\_element() do |child|  
logger.info “worksheet element text is ‘#{child.to\_s}’”  
child.each\_element() do |child\_2|  
logger.info “child element text is ‘#{child\_2.to\_s}’”  
end  
end

```
  @xml_document.elements.each("Workbook/DocumentProperties") do

```

|element|  
element.each\_element\_with\_text() do |child\_element|  
logger.info “Document Properties name =  
‘#{child\_element.text}’”  
end  
#Assessment.load\_from\_XML(element, @import\_issues)  
end  
else  
logger.info “Document Root is NIL!”  
end

I’m just trying to do some simple logging of the contents to feel my way  
around using this API. When I import the large file, the file\_obj is a  
File object. When I import the small file, file\_obj is a String object.  
So the code at the top is trying to account for that difference. Any  
thoughts would be appreciated.

Regards,  
Kevin

---

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [December 15, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/rexml-usage/79492/2 "2006-12-15T22:12:19Z")

</div>

Kevin T. wrote:

> When I import the large file, the file\_obj is a File object.  
> When I import the small file, file\_obj is a String object.

Here’s a snippet I’m using to allow a visitor to upload a file they’ve  
specified via a file\_field\_tag.

if params[:file\_to\_upload].instance\_of?(Tempfile)  
FileUtils.copy(params[:file\_to\_upload].local\_path,  
#{RAILS\_ROOT}/public/#{@filenametouse}")  
else  
File.open("#{RAILS\_ROOT}/public/#{@filenametouse}",“w”){|f|

f.write(params[:file\_to\_upload].read)

f.close}  
end

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [December 15, 2006, 10:16pm UTC](https://www.ruby-forum.com/t/rexml-usage/79492/3 "2006-12-15T22:16:20Z")

</div>

I should have noted, in case the point wasn’t clear from the code  
sample,

the string vs. file issue has nothing to do with REXML.

---

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [December 15, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/rexml-usage/79492/4 "2006-12-15T22:30:27Z")

</div>

bill walton wrote:

> I should have noted, in case the point wasn’t clear from the code  
> sample,
> 
> the string vs. file issue has nothing to do with REXML.

Understood…I wasn’t able to find an explanation of why sometimes I  
receive a file, and when I receive file contents.

As for the other issue (NIL root object), I tried the same sequence in  
irb and it loaded/parsed the file without issue. So I’m guessing it’s  
an issue with the Rails environment. It takes 3-4 seconds to parse when  
I open the file with irb, but the rails framework returns immediately,  
like it’s not even parsing the file. The development.log doesn’t seem  
to point to any issues.
