使用rails 3.0.7和ruby 1.9.2。
我正在使用Nokogiri构建自定义XML。我在主视图文件中调用部分构建器文件。但是部分用于xml的类不是Nokogiri。谁能解释一下为什么xml类不是Nokogiri ?
index.xml.erb
<%= Nokogiri::XML::Builder.new(:encoding => 'UTF-8') { |xml|
puts "xml class in main file #{xml.class}"
xml.users {
@users.each do |user|
xml << render(:partial=>"users/user", :locals=>{:user=>user, :xml => xml})
end
}
}.to_xml.html_safe
%>
_user.builder
puts "xml class in builder file #{xml.class}"
xml.user {
xml.id user.id
xml.name user.name
xml.email user.email
}
应用程序。Rb,使用:
ActiveSupport::XmlMini.backend = 'Nokogiri'
输出:
xml class in main file Nokogiri::XML::Builder
xml class in builder file Builder::XmlMarkup
IIUC ActiveSupport::XmlMini.backend = 'Nokogiri'
仅用于使用Nokogiri而不是ReXML解析XML参数,不用于生成构建器模板。
您正在手动创建Nokogiri::XML::Builder,然后依靠render
生成部分,因此您的输出是一致的。
使用以下内容(在config/initializers/nokogiri_builders.rb
中通过渲染实际使用Nokogiri
(加上您可以停止使用.xml.erb
文件来创建自己的Nokogiri::XML::Builder
实例,并且只使用.builder
):
# config/initializers/nokogiri_builders.rb
# Using Nokogiri::XML::Builder in lieu of stock pure ruby Builder for Rails builder templates
module Nokogiri
module XML
class Builder
# handle instruct! instead of emitting nonsense tag
def instruct!(tag, options = {})
case tag
when :xml
@version = options[:version]
@encoding = options[:encoding]
else
raise NotImplementedError(tag.inspect)
end
end
# redefined, to recover what instruct! gave us
def to_xml(*args)
if Nokogiri.jruby?
options = args.first.is_a?(Hash) ? args.shift : {}
unless options[:save_with]
options[:save_with] = Node::SaveOptions::AS_BUILDER
end
args.insert(0, options)
end
if @version || @encoding
# set options according to what was given previously
args[0][:version] = @version if @version
args[0][:encoding] = @encoding if @encoding
# do emit a declaration
args[0].delete(:save_with)
end
@doc.to_xml(*args)
end
end
end
end
module ActionView
module Template::Handlers
class Builder
# Default format used by Builder.
class_attribute :default_format
self.default_format = :xml
def call(template)
require_engine
code = <<-CODE
builder = Nokogiri::XML::Builder.new do |xml|
#{template.source}
end
# default to not emit a declaration (use instruct! instead)
save_options = Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
# default to UTF-8, as it's Rails default encoding
options = { encoding: 'UTF-8', indent: 2, save_with: save_options }
self.output_buffer = builder.to_xml(options)
CODE
code
end
protected
def require_engine
@required ||= begin
require 'nokogiri'
true
end
end
end
end
end
请注意,Nokogiri在使用to_xml
生成输出之前构建了一个完整的DOM,而Builder则动态地连接字符串。这可能是您想要的,也可能不是,特别是对于大型数据集。在这两种情况下,完整的final字符串都驻留在内存中。