In Files

Nokogiri

Nokogiri parses and searches XML/HTML very quickly, and also has correctly implemented CSS3 selector support as well as XPath support.

Parsing a document returns either a Nokogiri::XML::Document, or a Nokogiri::HTML::Document depending on the kind of document you parse.

Here is an example:

  require 'nokogiri'
  require 'open-uri'

  # Get a Nokogiri::HTML:Document for the page we’re interested in...

  doc = Nokogiri::HTML(open('http://www.google.com/search?q=tenderlove'))

  # Do funky things with it using Nokogiri::XML::Node methods...

  ####
  # Search for nodes by css
  doc.css('h3.r a.l').each do |link|
    puts link.content
  end

See Nokogiri::XML::Node#css for more information about CSS searching. See Nokogiri::XML::Node#xpath for more information about XPath searching.

Constants

LIBXML_PARSER_VERSION
(Not documented)
LIBXML_VERSION
(Not documented)
VERSION

The version of Nokogiri you are using

VERSION_INFO

More complete version information about libxml

Public Class Methods

HTML(thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block) click to toggle source

Parse HTML. Convenience method for Nokogiri::HTML::Document.parse

# File lib/nokogiri/html.rb, line 11
    def HTML thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML, &block
      Nokogiri::HTML::Document.parse(thing, url, encoding, options, &block)
    end
Hpricot(*args, &block) click to toggle source

Parse a document and apply the Hpricot decorators for Hpricot compatibility mode.

# File lib/nokogiri/hpricot.rb, line 52
    def Hpricot(*args, &block)
      if block_given?
        builder = Nokogiri::HTML::Builder.new(&block)
        Nokogiri::Hpricot.add_decorators(builder.doc)
      else
        doc = Nokogiri.parse(*args)
        Nokogiri::Hpricot.add_decorators(doc)
      end
    end
Slop(*args, &block) click to toggle source

Parse a document and add the Slop decorator. The Slop decorator implements method_missing such that methods may be used instead of CSS or XPath. For example:

  doc = Nokogiri::Slop(<<-eohtml)
    <html>
      <body>
        <p>first</p>
        <p>second</p>
      </body>
    </html>
  eohtml
  assert_equal('second', doc.html.body.p[1].text)
# File lib/nokogiri.rb, line 91
    def Slop(*args, &block)
      Nokogiri(*args, &block).slop!
    end
XML(thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_XML, &block) click to toggle source

Parse XML. Convenience method for Nokogiri::XML::Document.parse

# File lib/nokogiri/xml.rb, line 27
    def XML thing, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_XML, &block
      Nokogiri::XML::Document.parse(thing, url, encoding, options, &block)
    end
XSLT(stylesheet) click to toggle source

Create a Nokogiri::XSLT::Stylesheet with stylesheet.

Example:

  xslt = Nokogiri::XSLT(File.read(ARGV[0]))
# File lib/nokogiri/xslt.rb, line 12
    def XSLT stylesheet
      XSLT.parse(stylesheet)
    end
make(input = nil, opts = {}) click to toggle source

Create a new Nokogiri::XML::DocumentFragment

# File lib/nokogiri.rb, line 68
    def make input = nil, opts = {}, &blk
      if input
        Nokogiri::HTML.fragment(input).children.first
      else
        Nokogiri(&blk)
      end
    end
parse(string, url = nil, encoding = nil, options = nil) click to toggle source

Parse an HTML or XML document. string contains the document.

# File lib/nokogiri.rb, line 55
    def parse string, url = nil, encoding = nil, options = nil
      doc =
        if string =~ /^\s*<[^Hh>]*html/i # Probably html
          Nokogiri::HTML::Document.parse(string, url, encoding, options || XML::ParseOptions::DEFAULT_HTML)
        else
          Nokogiri::XML::Document.parse(string, url, encoding, options || XML::ParseOptions::DEFAULT_XML)
        end
      yield doc if block_given?
      doc
    end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.