XML文件不整齐

时间:2012-02-03 11:29:53

标签: xml ruby-on-rails-2

请有人告诉我这个的原因。我的控制器中有一个create_file动作。

   file = File.new("xml/experiment.xml", "w") 
   # creating the builder file for the xml
   xml = Builder::XmlMarkup.new :target => file
   # xml tags 
   xml.instruct! 
   xml.EXPERIMENT_SET { 
   xml.alias("#{@experiment.alias}") 
   xml.center_name "#{@experiment.center_name}"
   } 

我得到这样的输出。

   <?xml version="1.0" encoding="UTF-8"?><EXPERIMENT_SET><alias>ZAP430</alias><center_name></center_name></EXPERIMENT_‌​SET>

这不是一个简洁的XML,所有标签都显示在一行中。

1 个答案:

答案 0 :(得分:0)

在创建:indent实例时使用参数XmlMarkup

来自[Builder::XmlMarkup][1]的文档:

xm = Builder.new(:indent=>2)
# xm will produce nicely formatted and indented XML.

xm = Builder.new(:indent=>2, :margin=>4)
# xm will produce nicely formatted and indented XML with 2
# spaces per indent and an over all indentation level of 4.

builder = Builder::XmlMarkup.new(:target=>$stdout, :indent=>2)
builder.name { |b| b.first("Jim"); b.last("Weirich) }
# prints:
#     <name>
#       <first>Jim</first>
#       <last>Weirich</last>
#     </name>