Nokogiri XML构建器将不需要的“class ='call'”添加到我的XML中?

时间:2014-01-20 17:51:22

标签: ruby xml nokogiri

我必须向API发送测试请求以进行验证:

def fake_xml
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.TrackRequest.('USERID' => USPS_API["user_id"]) {
      xml.TrackID('ID' => 'EJ958083578US')
    }
  end
  puts builder.to_xml
  return builder.to_xml
end

但是,这会产生:

<TrackRequest class="call" USERID="xxxx">
  <TrackID ID="EJ958083578US"/>
</TrackRequest>

我不想要class="call",因为它会弄乱请求并导致它返回失败的响应。如何删除它或阻止Nokogiri自动添加该类?

1 个答案:

答案 0 :(得分:3)

尝试如下:

require 'nokogiri'

builder = Nokogiri::XML::Builder.new do |xml|
    xml.TrackRequest('USERID' => '12') {
      xml.TrackID('ID' => 'EJ958083578US')
    }
end

puts builder.to_xml
# >> <?xml version="1.0"?>
# >> <TrackRequest USERID="12">
# >>   <TrackID ID="EJ958083578US"/>
# >> </TrackRequest>

.应该在xml.TrackRequest.('USERID' => USPS_API["user_id"])。简单xml.TrackRequest('USERID' => USPS_API["user_id"])