Method: Nokogiri::XML::Node#write_to
- Defined in:
- lib/nokogiri/xml/node.rb
#write_to(io, *options) {|config| ... } ⇒ Object
:call-seq:
write_to(io, *)
Serialize this node or document to io
.
- Parameters
-
io
(IO) An IO-like object to which the serialized content will be written. -
options
(Hash) See below
- Options
-
:encoding
(String or Encoding) specify the encoding of the output (defaults to document encoding) -
:indent_text
(String) the indentation text (defaults to" "
) -
:indent
(Integer) the number of:indent_text
to use (defaults to2
) -
:save_with
(Integer) a combination of SaveOptions constants
To save with UTF-8 indented twice:
node.write_to(io, encoding: 'UTF-8', indent: 2)
To save indented with two dashes:
node.write_to(io, indent_text: '-', indent: 2)
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 |
# File 'lib/nokogiri/xml/node.rb', line 1440 def write_to(io, *) = .first.is_a?(Hash) ? .shift : {} encoding = [:encoding] || [0] || document.encoding if Nokogiri.jruby? = [:save_with] || [1] indent_times = [:indent] || 0 else = [:save_with] || [1] || SaveOptions::FORMAT indent_times = [:indent] || 2 end indent_text = [:indent_text] || " " # Any string times 0 returns an empty string. Therefore, use the same # string instead of generating a new empty string for every node with # zero indentation. indentation = indent_times.zero? ? "" : (indent_text * indent_times) config = SaveOptions.new(.to_i) yield config if block_given? encoding = encoding.is_a?(Encoding) ? encoding.name : encoding native_write_to(io, encoding, indentation, config.) end |