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

Inherits:
Object
  • Object
show all
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 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 SAX style parser included with Nokogiri. See Nokogiri::XML::SAX, and Nokogiri::HTML::SAX.

Direct Known Subclasses

FragmentHandler

Instance Method Summary collapse

Instance Method Details

#cdata_block(string) ⇒ Object

Called when cdata blocks are found string contains the cdata content



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

def cdata_block string
end

#characters(string) ⇒ Object

Characters read between a tag string contains the character data



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

def characters string
end

#comment(string) ⇒ Object

Called when comments are encountered string contains the comment data



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

def comment string
end

#end_documentObject

Called when document ends parsing



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

def end_document
end

#end_element(name) ⇒ Object

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



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

def end_element name
end

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

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



122
123
124
125
126
# File 'lib/nokogiri/xml/sax/document.rb', line 122

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

#error(string) ⇒ Object

Called on document errors string contains the error



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

def error string
end

#start_documentObject

Called when document starts parsing



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

def start_document
end

#start_element(name, attrs = []) ⇒ Object

Called at the beginning of an element name is the name of the tag with attrs as attributes



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

def start_element name, attrs = []
end

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

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



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/nokogiri/xml/sax/document.rb', line 105

def start_element_namespace name, attrs = [], prefix = nil, uri = nil, ns = []
  ###
  # Deal with SAX v1 interface
  name = [prefix, name].compact.join(':')
  attributes = ns.map { |ns_prefix,ns_uri|
    [['xmlns', ns_prefix].compact.join(':'), ns_uri]
  } + attrs.map { |attr|
    [[attr.prefix, attr.localname].compact.join(':'), attr.value]
  }.flatten
  start_element name, attributes
end

#warning(string) ⇒ Object

Called on document warnings string contains the warning



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

def warning string
end

#xmldecl(version, encoding, standalone) ⇒ Object

Called when an XML declaration is parsed



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

def xmldecl version, encoding, standalone
end