123456789_123456789_123456789_123456789_123456789_

Class: Nokogiri::XML::SAX::Document

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/nokogiri/xml/sax/document.rb

Overview

This class is used for registering types of events you are interested in handling. All of the methods on this class are available as possible events while parsing an ::Nokogiri::XML document. To register for any particular event, just subclass this class and implement the methods you are interested in knowing about.

To only be notified about start and end element events, write a class like this:

class MyDocument < Nokogiri::XML::SAX::Document
  def start_element name, attrs = []
    puts "#{name} started!"
  end

  def end_element name
    puts "#{name} ended"
  end
end

You can use this event handler for any ::Nokogiri::XML::SAX style parser included with ::Nokogiri. See ::Nokogiri::XML::SAX, and ::Nokogiri::HTML4::SAX.

Instance Method Summary

Instance Method Details

#cdata_block(string)

Called when cdata blocks are found string contains the cdata content

[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 155

def cdata_block(string)
end

#characters(string)

Characters read between a tag. This method might be called multiple times given one contiguous string of characters.

string contains the character data

[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 131

def characters(string)
end

#comment(string)

Called when comments are encountered string contains the comment data

[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 137

def comment(string)
end

#end_document

Called when document ends parsing

[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 79

def end_document
end

#end_element(name)

Called at the end of an element name is the tag name

[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 93

def end_element(name)
end

#end_element_namespace(name, prefix = nil, uri = nil)

Called at the end of an element name is the element’s name prefix is the namespace prefix associated with the element uri is the associated namespace URI

[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 120

def end_element_namespace(name, prefix = nil, uri = nil)
  ###
  # Deal with SAX v1 interface
  end_element([prefix, name].compact.join(":"))
end

#error(string)

Called on document errors string contains the error

[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 149

def error(string)
end

#processing_instruction(name, content)

Called when processing instructions are found name is the target of the instruction content is the value of the instruction

[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 162

def processing_instruction(name, content)
end

#start_document

Called when document starts parsing

[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 74

def start_document
end

#start_element(name, attrs = [])

Called at the beginning of an element

  • name is the name of the tag

  • attrs are an assoc list of namespaces and attributes, e.g.:

    [ ["xmlns:foo", "http://sample.net"], ["size", "large"] ]
[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 87

def start_element(name, attrs = [])
end

#start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = [])

Called at the beginning of an element name is the element name attrs is a list of attributes prefix is the namespace prefix for the element uri is the associated namespace URI ns is a hash of namespace prefix:urls associated with the element

[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 103

def start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = []) # rubocop:disable Metrics/ParameterLists
  ###
  # Deal with SAX v1 interface
  name = [prefix, name].compact.join(":")
  attributes = ns.map do |ns_prefix, ns_uri|
    [["xmlns", ns_prefix].compact.join(":"), ns_uri]
  end + attrs.map do |attr|
    [[attr.prefix, attr.localname].compact.join(":"), attr.value]
  end
  start_element(name, attributes)
end

#warning(string)

Called on document warnings string contains the warning

[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 143

def warning(string)
end

#xmldecl(version, encoding, standalone)

Called when an ::Nokogiri::XML declaration is parsed

[ GitHub ]

  
# File 'lib/nokogiri/xml/sax/document.rb', line 69

def xmldecl(version, encoding, standalone)
end