[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

REXML usage

Kevin Tambascio

12/15/2006 4:11:00 PM

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

--
Posted via http://www.ruby-....

3 Answers

Bill Walton

12/15/2006 9:07:00 PM

0


Kevin Tambascio 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

Bill Walton

12/15/2006 9:13:00 PM

0

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 walton wrote:
> Kevin Tambascio 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

Kevin Tambascio

12/15/2006 9:30:00 PM

0

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.

--
Posted via http://www.ruby-....