我正在使用 ::Builder::XmlMarkup ,如下所示:
# This is a class method in /lib, run ascynchronously using delayed_job.
# Bunch of file io / header stuff, then...
books.each do |book|
xml.Product do
xml.RecordReference "com.domain-#{book.id}"
xml.NotificationType book.notification unless book.notification.blank?
xml.ProductIdentifier do
xml.ProductIDType "15"
xml.IDValue book.isbn unless book.isbn.blank?
end
book.productcodes.each do |productcode|
unless productcode.idtype == "15"
xml.ProductIdentifier do
xml.ProductIDType productcode.idtype unless productcode.idtype.blank?
xml.IDValue productcode.idvalue unless productcode.idvalue.blank?
end
end
end
xml.ProductForm book.format unless book.format.blank?
xml.ProductFormDetail book.format_detail unless book.format_detail.blank?
xml.ProductFormDescription book.format_description unless book.format_description.blank?
等等。麻烦的是,它又持续了 250 行。这就是您要构建充满少量元素的 XML 文件的方式吗?它看起来不是很Rubyish。