rexml page.root.has_elements? says no elements

256 views Asked by At

I tried to read the following file:

with the code below:

require 'rexml/document'
include REXML

str = File.read("../pages/prac.xml").gsub(/\s+/, " ")

page = REXML::Document.new(str)
print "no elements\n" if page.root.has_elements?
print "Text: #{page.root.text}\n"
print "Name: #{page.root.name}\n"

page.root.each_element do |parent_tag|
    parent_tag.each_element do |tag|
        if tag.has_elements?
            tag.each_element do |data|
                p data
            end
        else
            puts "#{tag.name}: #{tag.text}"
        end
    end
end

The output I am seeing is:

no elements
Text:  
Name: html

Can someone help me by pointing out what is wrong here?

1

There are 1 answers

3
rbnewb On BEST ANSWER
print "no elements\n" if page.root.has_elements?

page.root.has_elements? returns true if the root element has child elements. In your case you are printing "no elements" when the root element finds child elements. It should probably read "has elements" instead as it is misleading as written.

Secondly, the output from page.root.name refers to the name of the root element of the XML document and hence prints out "html" in your case. However, page.root.text returns the first text node (not the text of a child element) which is probably a blank space and hence appears not to display anything.