Actions
Bug #10864
closedREXML adds white space to text property when indent > -1
Description
The following example shows that REXML is adding white space to the text property of elements when setting the :indent option on Document#write greater than -1. The value written should be the same value read.
require 'rexml/document'
xml_string = ''
REXML::Document.new(xml_string).tap do |doc|
doc.add_element('root').tap do |root|
write_text = 'bar'
puts "Write Text: #{write_text.inspect}"
root.add_element('foo').text = write_text
end
doc.write output: xml_string, indent: 4
end
REXML::Document.new(xml_string).tap do |doc|
read_text = doc.elements['/root/foo'].first
puts "Read Text: #{read_text.inspect}"
end
puts "XML String:"
puts xml_string
Write Text: "bar"
Read Text: "\n bar\n "
XML String:
<root>
<foo>
bar
</foo>
</root>
Updated by hsbt (Hiroshi SHIBATA) about 7 years ago
- Status changed from Open to Assigned
- Assignee set to kou (Kouhei Sutou)
Updated by kou (Kouhei Sutou) about 7 years ago
- Status changed from Assigned to Rejected
If you don't want to add extra spaces with indent, use transitive: true
option:
require 'rexml/document'
xml_string = ''
REXML::Document.new(xml_string).tap do |doc|
doc.add_element('root').tap do |root|
write_text = 'bar'
puts "Write Text: #{write_text.inspect}"
root.add_element('foo').text = write_text
end
doc.write output: xml_string, indent: 4, transitive: true
end
REXML::Document.new(xml_string).tap do |doc|
read_text = doc.elements['/root/foo'].first
puts "Read Text: #{read_text.inspect}"
end
puts "XML String:"
puts xml_string
Output:
Write Text: "bar"
Read Text: "bar"
XML String:
<root
><foo
>bar</foo
></root
>
Actions
Like0
Like0Like0