Reading xml file using REXML, says <UNDEFINED> ... </>
Asked Answered
T

1

5

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.

Tanker answered 15/10, 2012 at 2:15 Comment(0)
S
12

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.

Sankey answered 15/10, 2012 at 2:45 Comment(3)
It's been Nokogiri for several years now.Sankey
another strange thing, when I go to save it to a file, it is adding a <to_s/> tag at the end?Tanker
Why isn't article the root node? don't it contains all children?Elli

© 2022 - 2024 — McMap. All rights reserved.