Class: XML::SAX::Builder

Inherits:
Filter
  • Object
show all
Defined in:
lib/xml-sax-machines/builder.rb

Overview

Build a Nokogiri::XML::Document from a SAX stream.

Example

builder =  XML::SAX::Builder.new
parser  =  Nokogiri::XML::SAX::PushParser.new(builder)
parser  << %q{<root>xml content</root>}
parser.finish

puts builder.document.children.to_s #=> xml content

See

  • XML::SAX::Filter

– TODO:

  • Namespaces.

Direct Known Subclasses

FragmentBuilder

Instance Attribute Summary collapse

Attributes inherited from Filter

#filter

Instance Method Summary collapse

Methods inherited from Filter

#end_document, #error, #warning

Instance Attribute Details

#documentObject (readonly)

The document object.

Returns

Nokogiri::XML::Document



27
28
29
# File 'lib/xml-sax-machines/builder.rb', line 27

def document
  @document
end

Instance Method Details

#cdata_block(string) ⇒ Object

:nodoc:



66
67
68
69
# File 'lib/xml-sax-machines/builder.rb', line 66

def cdata_block(string) #:nodoc:
  super
  @context.add_child(Nokogiri::XML::CDATA.new(@document, string))
end

#characters(string) ⇒ Object

:nodoc:



55
56
57
58
59
60
61
62
63
64
# File 'lib/xml-sax-machines/builder.rb', line 55

def characters(string) #:nodoc:
  super
  # http://nokogiri.lighthouseapp.com/projects/19607-nokogiri/tickets/68-xpath-incorrect-when-text-siblings-exist#ticket-68-1
  sibling = @context.children.last
  if sibling.kind_of?(Nokogiri::XML::Text)
    sibling.content += string
  else
    @context.add_child(Nokogiri::XML::Text.new(string, @document))
  end
end

#comment(string) ⇒ Object

:nodoc:



71
72
73
74
# File 'lib/xml-sax-machines/builder.rb', line 71

def comment(string) #:nodoc:
  super
  @context.add_child(Nokogiri::XML::Comment.new(@document, string))
end

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

– TODO: Check namespace of context as well?



48
49
50
51
52
53
# File 'lib/xml-sax-machines/builder.rb', line 48

def end_element_namespace(name, prefix = nil, uri = nil)
  super
  raise "Unmatched closing element. Got '#{name}' but expected '#{@context.name}'" \
    unless name == @context.name
  @context = @context.parent
end

#start_documentObject

:nodoc:



29
30
31
32
33
# File 'lib/xml-sax-machines/builder.rb', line 29

def start_document #:nodoc:
  super
  @document = Nokogiri::XML::Document.new
  @context  = @document
end

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



35
36
37
38
39
40
41
42
43
44
# File 'lib/xml-sax-machines/builder.rb', line 35

def start_element_namespace(name, attributes = [], prefix = nil, uri = nil, ns = [])
  super
  el           = Nokogiri::XML::Element.new(name, @document)
  el.namespace = el.add_namespace_definition(prefix, uri) if uri
  ns.each{|prefix, href| el.add_namespace_definition(prefix, href)}
  @context = @context.add_child(el)

  # Node needs to be added to the document so namespaces are available.
  attributes.each{|attribute| el[[attribute.prefix, attribute.localname].compact.join(':')] = attribute.value}
end