given an xml string like this :
<some><nested><xml>value</xml></nested></some>
what's the best option(using ruby) to format it readable like :
<some>
<nested>
<xml>value</xml>
</nested>
</some>
I've found an answer here: what's the best way to format an xml string in ruby?, which is really helpful. But it formats xml like:
<some>
<nested>
<xml>
value
</xml>
</nested>
</some>
As my xml string is a little big in length. So it is not readable in this format.
Thanks in advance!
What about using nokogiri?
require 'nokogiri'
source = '<some><nested><xml>value</xml></nested></some>'
doc = Nokogiri::XML source
puts doc.to_xml
# <?xml version=\"1.0\"?>\n<some>\n <nested>\n <xml>value</xml>\n </nested>\n</some>\n
Use the REXML::Formatters::Pretty formatter:
require "rexml/document"
source = '<some><nested><xml>value</xml></nested></some>'
doc = REXML::Document.new(source)
formatter = REXML::Formatters::Pretty.new
# Compact uses as little whitespace as possible
formatter.compact = true
formatter.write(doc, $stdout)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With