Search code examples
rubyxmlxml-parsingmechanizexml-simple

How to parse XML with Mechanize and XMLSimple in ruby?


I'm trying to fetch a remote XML file with Mechanize to get icecast status information. But I'm having problems to pass the XML file from Mechanize::File format to string or some XML format which XMLSimple can work with.

The XML document looks like that:

<icestats>
  <admin>[email protected]</admin>
  <!-- ... -->
</icestats>

My code looks like that right now:

require 'mechanize'
require 'xmlsimple'

server = 'example.net'
port = 8000
user = 'stackoverflow'
password = 'hackme'
agent = Mechanize.new
agent.user_agent_alias = 'Linux Firefox'
agent.add_auth("http://#{server}:#{port}/admin/status.xml", user, password)
agent.get("http://#{server}:#{port}/admin/status.xml")
xml = agent.current_page
status = XmlSimple.xml_in(xml)
puts status['admin']

This should output: [email protected]

But it throws:

/home/user/.gem/ruby/1.9.1/gems/xml-simple-1.1.2/lib/xmlsimple.rb:191:in 'xml_in': Could not parse object of type: <Mechanize::File>. (ArgumentError)

Now, I understand the XMLSimple needs a string and therefore I tried to convert the Mechanize::File format to string, replacing the second last line with:

status = XmlSimple.xml_in(xml.to_s)

But this throws an even more weird exception:

/usr/lib64/ruby/1.9.1/rexml/parsers/baseparser.rb:406:in `block in pull_event': Undefined prefix Mechanize: found (REXML::UndefinedNamespaceException)
  from /usr/lib64/ruby/1.9.1/set.rb:222:in `block in each'
  from /usr/lib64/ruby/1.9.1/set.rb:222:in `each_key'
  from /usr/lib64/ruby/1.9.1/set.rb:222:in `each'
  from /usr/lib64/ruby/1.9.1/rexml/parsers/baseparser.rb:404:in `pull_event'
  from /usr/lib64/ruby/1.9.1/rexml/parsers/baseparser.rb:183:in `pull'
  from /usr/lib64/ruby/1.9.1/rexml/parsers/treeparser.rb:22:in `parse'
  from /usr/lib64/ruby/1.9.1/rexml/document.rb:231:in `build'
  from /usr/lib64/ruby/1.9.1/rexml/document.rb:43:in `initialize'
  from /home/user/.gem/ruby/1.9.1/gems/xml-simple-1.1.2/lib/xmlsimple.rb:965:in `new'
  from /home/user/.gem/ruby/1.9.1/gems/xml-simple-1.1.2/lib/xmlsimple.rb:965:in `parse'
  from /home/user/.gem/ruby/1.9.1/gems/xml-simple-1.1.2/lib/xmlsimple.rb:164:in `xml_in'
  from /home/user/.gem/ruby/1.9.1/gems/xml-simple-1.1.2/lib/xmlsimple.rb:203:in `xml_in'
  from debugging.rb:16:in `<main>'

What's wrong with my approach? When I download the XML file and use the local XML file the code above works as desired.

I'm especially looking for solutions with Mechanize rather than Nokogiri.


Solution

  • Try changing:

    xml = agent.current_page
    

    to:

    xml = agent.current_page.body