REXML usage

K

Kevin Tambascio

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
 
B

bill walton

Kevin said:
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
 
B

bill walton

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.

bill said:
Kevin said:
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
 
K

Kevin Tambascio

bill said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Errors on REXML reading an HTML. 1
REXML raw all doesn't seem to work 0
REXML Input File Question 7
help please with REXML 3
REXML Speed Question 3
Odd puts behaviour with REXML 7
REXML and Empty-Elements 1
REXML 19

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top