Class: Nokogiri::XML::Node

Inherits:
Object show all
Defined in:
lib/arachni/nokogiri/xml/node.rb

Instance Method Summary collapse

Instance Method Details

#serialize(*args, &block) ⇒ Object

Serialize Node using options. Save options can also be set using a block. See SaveOptions.

These two statements are equivalent:

node.serialize(:encoding => 'UTF-8', :save_with => FORMAT | AS_XML)

or

node.serialize(:encoding => 'UTF-8') do |config|
  config.format.as_xml
end


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/arachni/nokogiri/xml/node.rb', line 19

def serialize *args, &block
  options = args.first.is_a?(Hash) ? args.shift : {
    :encoding   => args[0],
    :save_with  => args[1] || SaveOptions::FORMAT
  }

  encoding = options[:encoding] || document.encoding

  outstring = ""
  if encoding && outstring.respond_to?(:force_encoding)
    begin
      outstring.force_encoding(Encoding.find(encoding))
    rescue
      outstring.force_encoding(Encoding.find('UTF-8'))
    end
  end
  io = StringIO.new(outstring)
  write_to io, options, &block
  io.string
end