Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7535957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:26:32+00:00 2026-05-30T06:26:32+00:00

I have to read xml files that are accessibles through http with authentication. That’s

  • 0

I have to read xml files that are accessibles through http with authentication. That’s why I use mechanize.

My problem is that I can’t get mechanize to recognize these XML files so I can use .find or .search on them.

Here is what I tried first – in my view (html file)

<% agent = Mechanize.new %>

<% page = agent.get("http://dl.dropbox.com/u/344349/xml.xml") %>

<%= page %>

Which returns #<Mechanize::File:0x007f9dd602de30>. It’s ::File and not ::Page
I can’t use a .find or .search on this as it’ll error with undefined method find for #<Mechanize::File:0x007f9dd624cbd0>

Mechanize doc says : This is the default (and base) class for the Pluggable Parsers. If Mechanize cannot find an appropriate class to use for the content type, this class will be used. For example, if you download a JPG, Mechanize will not know how to parse it, so this class will be instantiated.

So I created a class as described here : http://rdoc.info/github/tenderlove/mechanize/master/Mechanize/PluggableParser

My class

class XMLParser < Mechanize::File

attr_reader :xml

def initialize(uri=nil, response=nil, body=nil, code=nil)

super(uri, response, body, code)

@xml = xml.parse(body)

end

end

and the updated code in my view (html file)

<% agent = Mechanize.new %>

<% agent.pluggable_parser['text/xml'] = XMLParser %>

<% agent.user_agent_alias = 'Windows Mozilla' %>

<% page = agent.get("http://dl.dropbox.com/u/344349/xml.xml") %>

<%= page %>

or even

<% agent = Mechanize.new %>

<% agent.pluggable_parser.xml = XMLParser %>

<% page1 = agent.get('http://dl.dropbox.com/u/344349/xml.xml') # => CSVParser %>

<%= page1 %>

Still returns #<Mechanize::File:0x007f9dd5253b48>

I even tested the exact code (CSVParser – http://rdoc.info/github/tenderlove/mechanize/master/Mechanize/PluggableParser) and tried loading a csv file that is still seen as a ::File.

What am I doing wrong ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:26:34+00:00Added an answer on May 30, 2026 at 6:26 am

    Okay, so I’ve resolved this problem for myself just now. The solution is in two parts:

    First, the content type you are matching is incorrect. If you run this line, after you do your get, it will tell you what the content type is for the document you are getting:

    page.response['content-type'] # => 'application/xml', not 'text/xml'
    

    When I use mechanize to get your page (‘http://dl.dropbox.com/u/344349/xml.xml’), I see ‘application/xml’ as the content type.

    Second, you’re not using PluggableParser correctly. Using XMLParser as you have it here will generate NoMethodError: undefined method 'parse' for nil:NilClass. Change the class definition to use Nokogiri::XML instead:

    class XmlParser < Mechanize::File
      attr_reader :xml
      def initialize(uri = nil, response = nil, body = nil, code = nil)
        @xml = Nokogiri::XML(body)
        super uri, response, body, code
      end
    end
    

    Then, set this as the parser for the correct content type:

    mech.pluggable_parser['application/xml'] = XmlParser
    

    To use this, you’ll get your page the same as before, and then reference the xml attribute of the page object as a Nokogiri::XML::Document instance, which is a subclass of Nokogiri::XML::Node. Fortunately, Mechanize::Page.search is just a wrapper around Nokogiri::XML::Node.search, so you can search the same way you expect, pretty much. Like this:

    page.xml.search 'catalog'
    

    A further refinement would be to map XmlParser.search to the Nokogiri .search methods:

    # This is the same as what Mechanize::Page does
    class XmlParser < Mechanize::File
      extend Forwardable
      def_delegators  :@xml, :search, :/, :at
    end
    

    This lets you perform your searches directly on the page instance:

    page.search 'catalog'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some classes that already use DOM4J to read XML files and provide
I have several XML files that I wish to read attributes from. My main
I know that XML Simple will read xml files but what if i have
I've read a little about KML files. They are XML files that can be
I have xml files that I read in at runtime, is is possible to
I wish to read incoming XML files that have no specific name (e.g. date/time
I have some xml files that will be read-only that I need to access
I have been using JDOM library to read and write XML files through Java
I have a really simple XML file that I'm trying to read, but I
I have xml files I want to read in and store in the database.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.