I have a very simple xml file that I am trying to access:
<article>
<text>hello world</text>
</article>
I'm doing this so far:
file = File.open("#{Rails.root}/public/files/#{file_id}.xml", "r")
xml = file.read
doc = REXML::Document.new(xml)
When I run this code in rails console, I see:
1.9.3-p194 :033 > doc.inspect
=> "<UNDEFINED> ... </>"
I can't seem to understand why it is not loading the file correctly, I can't access the text xml element either.
It is loading correctly, the document just doesn't have a root node.
require "rexml/document"
doc = REXML::Document.new DATA.read
doc.root_node # => <UNDEFINED> ... </>
doc.inspect # => "<UNDEFINED> ... </>"
doc.to_s # => "<article>\n <text>hello world</text>\n</article>\n"
doc.get_elements('//article') # => [<article> ... </>]
doc.get_elements('//text') # => [<text> ... </>]
__END__
<article>
<text>hello world</text>
</article>
By the way, I think the Ruby community has pretty much universally endorsed Nokogiri for xml parsing.