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 6934293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:56:28+00:00 2026-05-27T11:56:28+00:00

<root> <channel> <one>example</one> <two>example2</two> </channel> <channel> <one>example</one> </channel> </root> In the second node, I

  • 0
<root>
  <channel>
    <one>example</one>
    <two>example2</two>
  </channel>
  <channel>
    <one>example</one>
  </channel>
</root>

In the second node, I don’t have a <two> node. If I use this: root.channel.two obviously I get the error “Method missing”. How can I check to avoid this error? What is the conditional statement I would use?

  • 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-27T11:56:29+00:00Added an answer on May 27, 2026 at 11:56 am

    Technique 1: Rescue Any Error

    require 'nokogiri'
    d = Nokogiri.XML("<foo><bar /></foo>")
    bad = d.root.bar            #=> undefined method `bar' for #<...> (NoMethodError)
    d.slop!           
    yay = d.root.bar            #=> #<... name="bar">
    bad = d.root.xxx            #=> undefined method `xxx' for #<...> (NoMethodError)
    yay = d.root.xxx rescue nil #=> nil
    

    Technique 2: Look Before Leaping (aka Don’t Use Slop)

    %w[ bar xxx ].each do |node_name|
      if n = d.root.at_xpath(node_name)
        puts "Yay! #{n}"
      else
        puts "No node named #{node_name}"
      end
    end
    #=> Yay! <bar/>
    #=> No node named xxx
    

    The (no-slop) code some_node.at_xpath("foo") is identical to some_node.foo when using slop, except that it returns nil when no child node with that name exists. Indeed, the implementation of Slop just calls xpath for the element name: if it finds many elements, you get that Nodeset; if it finds only one element, it gives you that; if it finds no elements, it raises the NoMethodError. The important bits look like this:

    def method_missing( name )
      list = xpath(name)
      if list.empty?
        super                 # NoMethodError unless someone else handles this
      elsif list.length == 1
        list.first            # Since we only found one element, return that
      else
        list                  # ...otherwise return the whole list
      end
    end
    

    Here’s what the Nokogiri documents say about Slop (in the footnotes):

    Don’t use this.
    No, really, don’t use this. If you use it, don’t report bugs.
    You’ve been warned!

    In general, XPath is way more powerful and faster than slop traversal. For example, if you want to iterate over every <two> node, you can do:

    d.xpath('/root/channel/two').each do |two|
      # This will only find nodes that exist
    end
    

    If you describe what you really need to do in the end, we can help you craft better code. In my personal opinion Slop is generally a less-effective way to traverse a document.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am installing phing using instructions here I get the following error. I have
I'm trying to debug why I get this error: Error occurred while sending a
ok, i have searched for this in the past two hours with results that
I'm trying to get channel element from this document. <rdf:RDF xmlns:rdf=http://www.w3.org/1999/02/22-rdf-syntax-ns# xmlns=http://purl.org/rss/1.0/ xmlns:slash=http://purl.org/rss/1.0/modules/slash/ xmlns:taxo=http://purl.org/rss/1.0/modules/taxonomy/
Root view is a UIImage View, it has subviews, those have subviews. My root
For example: root.Nodes.Add(new TNode() { Foo1 = bar1, Foo2 = bar2, Foo3 = bar3
This is part of my TV guide script: //Connect to the database mysql_connect(localhost,root,PASSWORD); //Select
Does the channel file need to be in the root of the site or
I have a CoreData database which works on the root view controller. I have
I have the two following lists: # List of tuples representing the index of

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.