Nokogiri to_xml 不带回车符

发布于 2024-12-20 03:11:47 字数 479 浏览 4 评论 0原文

我目前正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瀟灑尐姊 2024-12-27 03:11:47

Builder#to_xml 默认输出格式化(即缩进)的 XML。您可以使用 Nokogiri::XML::Node:: SaveOptions 以获得几乎未格式化的结果。

b = Nokogiri::XML::Builder.new do |xml|
  xml.root do
    xml.foo do
      xml.text("Value")
    end
  end
end

b.to_xml
# => "<?xml version=\"1.0\"?>\n<root>\n  <foo>Value</foo>\n</root>\n"

b.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML)
# => "<?xml version=\"1.0\"?>\n<root><foo>Value</foo></root>\n"

现在,您可以删除 XML 标头(无论如何都是可选的)并删除最后一个换行符

b.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip
# => "<root><foo>Value</foo></root>"

仅仅删除 XML 中的所有换行符可能是一个坏主意,因为换行符实际上可能很重要(例如在

; 中)。  XHTML 块)。如果您的情况不是这样(并且您确实确信这一点),您可以这样做。

Builder#to_xml by default outputs formatted (i.e. indented) XML. You can use the Nokogiri::XML::Node::SaveOptions to get an almost unformatted result.

b = Nokogiri::XML::Builder.new do |xml|
  xml.root do
    xml.foo do
      xml.text("Value")
    end
  end
end

b.to_xml
# => "<?xml version=\"1.0\"?>\n<root>\n  <foo>Value</foo>\n</root>\n"

b.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML)
# => "<?xml version=\"1.0\"?>\n<root><foo>Value</foo></root>\n"

Now you could either just get rid of the XML header (which is optional anyway) and remove the last newline

b.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip
# => "<root><foo>Value</foo></root>"

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.

一刻暧昧 2024-12-27 03:11:47

这不是 Nokogiri 设计的初衷。您可以获得的最接近的方法是序列化文档的根目录,不带换行符或缩进,然后自己添加 PI(如果您确实需要它):

require 'nokogiri'

b = Nokogiri::XML::Builder.new{ |xml| xml.root{ xml.foo "Value" } }
p b.to_xml
#=> "<?xml version=\"1.0\"?>\n<root>\n  <foo>Value</foo>\n</root>\n"

p b.doc.serialize(save_with:0)
#=> "<?xml version=\"1.0\"?>\n<root><foo>Value</foo></root>\n"

flat_root = b.doc.root.serialize(save_with:0)
p flat_root
#=> "<root><foo>Value</foo></root>"

puts %Q{<?xml version="1.0"?>#{flat_root}}
#=> <?xml version="1.0"?><root><foo>Value</foo></root>

或者,您可以简单地作弊并执行以下操作:

puts b.doc.serialize(save_with:0).sub("\n","")
#=> <?xml version="1.0"?><root><foo>Value</foo></root>

注意 < 的用法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):

require 'nokogiri'

b = Nokogiri::XML::Builder.new{ |xml| xml.root{ xml.foo "Value" } }
p b.to_xml
#=> "<?xml version=\"1.0\"?>\n<root>\n  <foo>Value</foo>\n</root>\n"

p b.doc.serialize(save_with:0)
#=> "<?xml version=\"1.0\"?>\n<root><foo>Value</foo></root>\n"

flat_root = b.doc.root.serialize(save_with:0)
p flat_root
#=> "<root><foo>Value</foo></root>"

puts %Q{<?xml version="1.0"?>#{flat_root}}
#=> <?xml version="1.0"?><root><foo>Value</foo></root>

Alternatively, you could simply cheat and do:

puts b.doc.serialize(save_with:0).sub("\n","")
#=> <?xml version="1.0"?><root><foo>Value</foo></root>

Note the usage of sub instead of gsub to only replace the first known-present newline.

没有心的人 2024-12-27 03:11:47

b.to_xml 返回一个字符串。您只需替换字符串中的第一个 \n 实例即可。

require 'nokogiri'

b = Nokogiri::XML::Builder.new do |xml|
  xml.root do
    xml.text("Value")
  end
end

b.to_xml.sub("\n",'')

可能比尝试重载该方法更容易。

b.to_xml returns a string. You just need to replace the first instance of \n in the string.

require 'nokogiri'

b = Nokogiri::XML::Builder.new do |xml|
  xml.root do
    xml.text("Value")
  end
end

b.to_xml.sub("\n",'')

Probably easier than trying to overload the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文