CodeBiker
CodeBiker

Reputation: 3263

The document "doc.xml" does not have a valid root (REXML::ParseException)

I'm trying to convert an XML document into a Ruby hash for the first time, and having no success. I have my XML document, doc.xml, in a folder along with my script hashrunner.rb.

In hashrunner.rb:

require 'active_support/core_ext/hash'
hash = Hash.from_xml("doc.xml")
puts hash

The first line of the XML document is <?xml version="1.0" encoding="US-ASCII"?>, if that is helpful.

In my console, when I run ruby hashrunner.rb, I get the error message:

/Users/me/.rvm/gems/ruby-1.9.3-p374/gems/activesupport-4.0.0/lib/active_support/xml_mini/rexml.rb:34:in `parse':The document "doc.xml" does not have a valid root (REXML::ParseException)

As someone relatively new to Ruby, I don't understand what this means, and some internet searching didn't turn up an explanation, either. To start, I'm not even sure if I'm calling the XML file correctly in the from_xml method, so please let me know if that's the case. I'd be open to using different gems or a different approach if that would help.

Upvotes: 2

Views: 3286

Answers (1)

Jacob Brown
Jacob Brown

Reputation: 7561

I'm pretty sure Hash::from_xml has to take an XML string, not a filename string. Try:

hash = Hash.from_xml(File.read("doc.xml"))

Upvotes: 10

Related Questions