I’m currently using the Nokogiri::XML::Builder class to construct an XML document, then calling .to_xml on it. The resulting string always contains a bunch of spaces, linefeeds and carriage returns in between the nodes, and I can’t for the life of me figure out how to get rid of them. Here’s an example:
b = Nokogiri::XML::Builder.new do |xml|
xml.root do
xml.text("Value")
end
end
b.to_xml
This results in the following:
<?xml version="1.0"?>
<root>Value</root>
What I want is this (notice the missing newline):
<?xml version="1.0"?><root>Value</root>
How can this be done? Thanks in advance!
Builder#to_xmlby default outputs formatted (i.e. indented) XML. You can use theNokogiri::XML::Node::SaveOptionsto get an almost unformatted result.Now you could either just get rid of the XML header (which is optional anyway) and remove the last newline
Just removing all newlines in the XML is probably a bad idea as newlines can actually be significant (e.g. in
<pre>blocks of XHTML). If that is not the case for you (and you are really sure of that) you could just do it.