Method: Nokogiri::XML::Node#serialize

Defined in:
lib/nokogiri/xml/node.rb

#serialize(*args, &block) ⇒ Object

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

See also Nokogiri::XML::Node::SaveOptions and Node@Serialization+and+Generating+Output.

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
[View source]

1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
# File 'lib/nokogiri/xml/node.rb', line 1364

def serialize(*args, &block)
  # TODO: deprecate non-hash options, see 46c68ed 2009-06-20 for context
  options = if args.first.is_a?(Hash)
    args.shift
  else
    {
      encoding: args[0],
      save_with: args[1],
    }
  end

  options[:encoding] ||= document.encoding
  encoding = Encoding.find(options[:encoding] || "UTF-8")

  io = StringIO.new(String.new(encoding: encoding))

  write_to(io, options, &block)
  io.string
end