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:
ocument.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
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:
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