2
require 'rexml/document'
str = File.readlines('myxml.xml').join # '<a></a>'
el = REXML::Document.new str
p el.to_s # "<a/>"

I want <a></a> instead of <a />. How can I get this with rexml in ruby?

Mike
  • 395
  • 2
  • 4
  • 14

1 Answers1

1

Since it’s an XML, the elements got collapsed unless there is a [possibly emply] node nested:

el.root.add_text ''
el.to_s
#⇒ "<a></a>"
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • Is there a way to keep the origin rather than modify it? Assume I am parsing an xml file, it is not applicable to inspect all nodes, right? – Mike Mar 16 '17 at 01:55