Nokogiri to_xml 不带回车符
我目前正在使用 Nokogiri::XML::Builder 类来构造 XML 文档,然后在其上调用 .to_xml 。生成的字符串在节点之间总是包含一堆空格、换行符和回车符,而且我一辈子都无法弄清楚如何摆脱它们。这是一个例子:
b = Nokogiri::XML::Builder.new do |xml|
xml.root do
xml.text("Value")
end
end
b.to_xml
这会产生以下结果:
<?xml version="1.0"?>
<root>Value</root>
我想要的是这个(注意缺少的换行符):
<?xml version="1.0"?><root>Value</root>
这是如何做到的?提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Builder#to_xml
默认输出格式化(即缩进)的 XML。您可以使用Nokogiri::XML::Node:: SaveOptions
以获得几乎未格式化的结果。现在,您可以删除 XML 标头(无论如何都是可选的)并删除最后一个换行符
仅仅删除 XML 中的所有换行符可能是一个坏主意,因为换行符实际上可能很重要(例如在
Builder#to_xml
by default outputs formatted (i.e. indented) XML. You can use theNokogiri::XML::Node::SaveOptions
to 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.这不是 Nokogiri 设计的初衷。您可以获得的最接近的方法是序列化文档的根目录,不带换行符或缩进,然后自己添加 PI(如果您确实需要它):
或者,您可以简单地作弊并执行以下操作:
注意 < 的用法code>sub 而不是
gsub
仅替换第一个已知存在的换行符。This is not something that Nokogiri is designed to do. The closest you can get is to serialize the root of the document with no newlines or indentation, and then add the PI yourself (if you really need it):
Alternatively, you could simply cheat and do:
Note the usage of
sub
instead ofgsub
to only replace the first known-present newline.b.to_xml
返回一个字符串。您只需替换字符串中的第一个\n
实例即可。可能比尝试重载该方法更容易。
b.to_xml
returns a string. You just need to replace the first instance of\n
in the string.Probably easier than trying to overload the method.