Nokogiri XML创建字段名称需要帮助 - Ruby 1.9

时间:2013-01-09 18:18:25

标签: ruby nokogiri

我正在尝试使用合作伙伴提供给我的规范来创建XML文档,并且我在创建指定的XML字段名称时遇到了一些问题。我们通常会创建字段名称,因此输出显示如下所示。

def build_xml(output_rows)
builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|     
xml.BATCH{
  xml.HEADER{
      xml.CBPosterEmail ("test@foo.com")
      xml.CBPosterPassword ('123456')
      xml.CBReplace ('Yes')
      xml.CBReplyTo ('replytome@foo.com')
  } #xml HEADER


<Header>
<CBEmail>test@foo.com</CBEmail>
<CBPosterPassword>123456</CBPosterPassword>
<CBReplace>Yes</CBReplace>
<CBReplyTo>replytome@foo.com</CBReplyTo>
</Header>

我需要在输出中显示字段名称,如下例所示:

<Header>
<Field name="CBPosterEmail" value="test@foo.com" />
<Field name="CBPosterPassword" value="123456" />
<Field name=”CbReplace” value=”Yes”>
<Field name=”CBReplyTo” value=”replytome@foo.com”>
</Header>

当我尝试添加Field name =“和value = piece时,我收到一个错误。我是Nokogiri的新手,所以如果我听起来像个完全白痴,请放轻松。任何帮助都非常感谢

2 个答案:

答案 0 :(得分:3)

require 'nokogiri'
builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|
xml.batch {
  xml.header {
    xml.field(:name => "CBPosterEmail", :value => "test@foo.com")
    xml.field(:name => "CBPosterPassword", :value => "123456")
    xml.field(:name => "CBReplace", :value => "Yes")
    xml.field(:name => "CBReplyTo", :value => "replytome@foo.com")
  }
}
end
puts builder.to_xml

=> <?xml version="1.0" encoding="utf-8"?>
<batch>
  <header>
    <field name="CBPosterEmail" value="test@foo.com"/>
    <field name="CBPosterPassword" value="123456"/>
    <field name="CBReplace" value="Yes"/>
    <field name="CBReplyTo" value="replytome@foo.com"/>
  </header>
</batch>

答案 1 :(得分:2)

请参阅Nokogiri Builder文档的“Tag attributes”部分。

builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|     
  xml.BATCH{
    xml.HEADER{
      xml.Field( name: 'CBPosterEmail',    value: 'test@foo.com'      )
      xml.Field( name: 'CBPosterPassword', value: '123456'            )
      xml.Field( name: 'CBReplace',        value: 'Yes'               )
      xml.Field( name: 'CBReplyTo',        value: 'replytome@foo.com' )
    }
  }
end

puts builder.to_xml

结果是:

<?xml version="1.0" encoding="utf-8"?>
<BATCH>
  <HEADER>
    <Field name="CBPosterEmail" value="test@foo.com"/>
    <Field name="CBPosterPassword" value="123456"/>
    <Field name="CBReplace" value="Yes"/>
    <Field name="CBReplyTo" value="replytome@foo.com"/>
  </HEADER>
</BATCH>

示例代码显示您正在尝试添加<BATCH>标记,但所需的输出示例未显示该标记。如果输出样本正确,则会生成:

builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |xml|     
  xml.HEADER{
    xml.Field( name: 'CBPosterEmail',    value: 'test@foo.com'      )
    xml.Field( name: 'CBPosterPassword', value: '123456'            )
    xml.Field( name: 'CBReplace',        value: 'Yes'               )
    xml.Field( name: 'CBReplyTo',        value: 'replytome@foo.com' )
  }
end

结果是:

<?xml version="1.0" encoding="utf-8"?>
<HEADER>
  <Field name="CBPosterEmail" value="test@foo.com"/>
  <Field name="CBPosterPassword" value="123456"/>
  <Field name="CBReplace" value="Yes"/>
  <Field name="CBReplyTo" value="replytome@foo.com"/>
</HEADER>