如何使用Ruby和Builder :: XMLMarkup模板生成XML文件?

时间:2022-11-23 08:36:07

As you all know, with Rails it is possible to use Builder::XMLMarkup templates to provide an http reponse in XML format instead of HTML (with the respond_to command). My problem is that I would like to use the Builder::XMLMarkup templating system not with Rails but with Ruby only (i.e. a standalone program that generates/outputs an XML file from an XML template). The question is then twofold:

众所周知,使用Rails可以使用Builder :: XMLMarkup模板以XML格式而不是HTML(使用respond_to命令)提供http响应。我的问题是我想使用Builder :: XMLMarkup模板系统而不是Rails,而只使用Ruby(即一个从XML模板生成/输出XML文件的独立程序)。问题是双重的:

  1. How do I tell the Ruby program which is the template I want to use? and
  2. 如何告诉Ruby程序我想要使用哪个模板?和

  3. How do I tell the Builder class which is the output XML file ?
  4. 如何告诉Builder类哪个是输出XML文件?

There is already a similar answer to that in * (How do I generate XML from XMLBuilder using a .xml.builder file?), but I am afraid it is only valid for Rails.

在*中已经有类似的答案(如何使用.xml.builder文件从XMLBuilder生成XML?),但我担心它只适用于Rails。

2 个解决方案

#1


7  

Here's a simple example showing the basics:

这是一个显示基础知识的简单示例:

require 'builder'

@received_data = {:books => [{ :author => "John Doe", :title => "Doeisms" }, { :author => "Jane Doe", :title => "Doeisms II" }]}
@output = ""
xml = Builder::XmlMarkup.new(:target => @output, :indent => 1)

xml.instruct!
xml.books do
  @received_data[:books].each do |book|
    xml.book do
      xml.title book[:title]
      xml.author book[:author]
    end
  end
end

The @output object will contain your xml markup:

@output对象将包含您的xml标记:

<?xml version="1.0" encoding="UTF-8"?>
<books>
 <book>
  <title>Doeisms</title>
  <author>John Doe</author>
 </book>
 <book>
  <title>Doeisms II</title>
  <author>Jane Doe</author>
 </book>
</books>

The Builder docs at github.com provide more examples and links to more documentation.

github.com上的Builder文档提供了更多示例和更多文档的链接。

To select a specific template, you could pass arguments to your program for this decision.

要选择特定模板,您可以将参数传递给程序以进行此决策。

Anyway, I prefer to use libxml-ruby to parse and build XML documents, but that's a matter of taste.

无论如何,我更喜欢使用libxml-ruby来解析和构建XML文档,但这是一个品味问题。

#2


1  

I used Tilt to do (the first part of) this. It's really easy:

我使用Tilt来做(第一部分)。这真的很容易:

require 'builder'
require 'tilt'
template = Tilt.new('templates/foo.builder')
output = template.render

That will get you a string representation of your xml. At that point you can write it out to disk yourself.

这将为您提供xml的字符串表示形式。此时,您可以自己将其写入磁盘。

#1


7  

Here's a simple example showing the basics:

这是一个显示基础知识的简单示例:

require 'builder'

@received_data = {:books => [{ :author => "John Doe", :title => "Doeisms" }, { :author => "Jane Doe", :title => "Doeisms II" }]}
@output = ""
xml = Builder::XmlMarkup.new(:target => @output, :indent => 1)

xml.instruct!
xml.books do
  @received_data[:books].each do |book|
    xml.book do
      xml.title book[:title]
      xml.author book[:author]
    end
  end
end

The @output object will contain your xml markup:

@output对象将包含您的xml标记:

<?xml version="1.0" encoding="UTF-8"?>
<books>
 <book>
  <title>Doeisms</title>
  <author>John Doe</author>
 </book>
 <book>
  <title>Doeisms II</title>
  <author>Jane Doe</author>
 </book>
</books>

The Builder docs at github.com provide more examples and links to more documentation.

github.com上的Builder文档提供了更多示例和更多文档的链接。

To select a specific template, you could pass arguments to your program for this decision.

要选择特定模板,您可以将参数传递给程序以进行此决策。

Anyway, I prefer to use libxml-ruby to parse and build XML documents, but that's a matter of taste.

无论如何,我更喜欢使用libxml-ruby来解析和构建XML文档,但这是一个品味问题。

#2


1  

I used Tilt to do (the first part of) this. It's really easy:

我使用Tilt来做(第一部分)。这真的很容易:

require 'builder'
require 'tilt'
template = Tilt.new('templates/foo.builder')
output = template.render

That will get you a string representation of your xml. At that point you can write it out to disk yourself.

这将为您提供xml的字符串表示形式。此时,您可以自己将其写入磁盘。