在轨道4中使用Nokogiri构建xml的问题

时间:2013-12-11 16:48:11

标签: ruby-on-rails xml nokogiri

我需要构建以下xml数据

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:description7a.services.chrome.com">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:VehicleDescriptionRequest>
         <urn:accountInfo number="test" secret="test" country="US" language="en" behalfOf="test"/>
         <!--You have a CHOICE of the next 3 items at this level-->
         <!--Optional:-->
         <urn:styleId>test</urn:styleId>
         <urn:switch>ShowAvailableEquipment</urn:switch>
         <urn:includeMediaGallery>Both</urn:includeMediaGallery>
      </urn:VehicleDescriptionRequest>
   </soapenv:Body>
</soapenv:Envelope>

我正在使用以下代码构建它

def self.xml_build(style_id,number,secret,behalfOf)
    builder = Nokogiri::XML::Builder.new do |xml|
      xml.send("soapenv:Envelope"){
      xml.send("soapenv:Header")
        xml.send("soapenv:Body") {
          xml.send("urn:VehicleDescriptionRequest"){
            xml.send("urn:accountInfo")("number"=>"#{number}" "secret"=>"#{secret}" "country"=>"US" "language"=>"en" "behalfOf"=>"#{behalfOf}")
            xml.send("urn:styleId"){xml.text "#{style_id}"}
            xml.send("urn:switch"){xml.text "ShowAvailableEquipment"}
            xml.send("urn:includeMediaGallery"){xml.text "Both"}
            }
          } 
        }
    end
    file_test=File.new("dummy_file", 'w')
    file_test.puts builder.to_xml
    builder.to_xml
end

我收到以下错误

SyntaxError: /home/aravind/Documents/dev/nthat/app/models/carinfo.rb:27: syntax error, unexpected '(', expecting '}'
...   xml.send("urn:accountInfo")("number"=>"#{number}" "secret...
...                               ^
/home/aravind/Documents/dev/nthat/app/models/carinfo.rb:27: syntax error, unexpected =>, expecting '}'
...d("urn:accountInfo")("number"=>"#{number}" "secret"=>"#{secr...
...                               ^
/home/aravind/Documents/dev/nthat/app/models/carinfo.rb:27: syntax error, unexpected =>, expecting '}'
...number"=>"#{number}" "secret"=>"#{secret}" "country"=>"US" "...
...                               ^
/home/aravind/Documents/dev/nthat/app/models/carinfo.rb:27: syntax error, unexpected =>, expecting '}'
...ecret"=>"#{secret}" "country"=>"US" "language"=>"en" "behalf...
...                               ^
/home/aravind/Documents/dev/nthat/app/models/carinfo.rb:27: syntax error, unexpected =>, expecting '}'
...}" "country"=>"US" "language"=>"en" "behalfOf"=>"#{behalfOf}...
...                               ^
/home/aravind/Documents/dev/nthat/app/models/carinfo.rb:27: syntax error, unexpected =>, expecting '}'
..." "language"=>"en" "behalfOf"=>"#{behalfOf}")

另外我不确定如何构建xml的这一部分:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:description7a.services.chrome.com">

1 个答案:

答案 0 :(得分:0)

以下将生成您的xml:

require 'nokogiri'    
def self.xml_build(style_id,number,secret,behalfOf)    
    builder = Nokogiri::XML::Builder.new do |xml|
      xml.send("soapenv:Envelope", 'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/", 'xmlns:urn' => "urn:description7a.services.chrome.com"){
      xml.send("soapenv:Header")
        xml.send("soapenv:Body") {
          xml.send("urn:VehicleDescriptionRequest"){
          xml.send("urn:accountInfo", "number"=>"#{number}", "secret"=>"#{secret}", "country"=>"US", "language"=>"en", "behalfOf"=>"#{behalfOf}")
            xml.comment('You have a CHOICE of the next 3 items at this level')
            xml.comment('Optional:')
            xml.send("urn:styleId"){xml.text "#{style_id}"}
            xml.send("urn:switch"){xml.text "ShowAvailableEquipment"}
            xml.send("urn:includeMediaGallery"){xml.text "Both"}
            }
          } 
        }
     end
end
puts xml_build('test', 'test', 'test', 'test').to_xml
#=> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:description7a.services.chrome.com">
#=>   <soapenv:Header/>
#=>   <soapenv:Body>
#=>     <urn:VehicleDescriptionRequest>
#=>       <urn:accountInfo number="test" secret="test" country="US" language="en" behalfOf="test"/>
#=>       <!--You have a CHOICE of the next 3 items at this level-->
#=>       <!--Optional:-->
#=>       <urn:styleId>test</urn:styleId>
#=>       <urn:switch>ShowAvailableEquipment</urn:switch>
#=>       <urn:includeMediaGallery>Both</urn:includeMediaGallery>
#=>     </urn:VehicleDescriptionRequest>
#=>   </soapenv:Body>
#=> </soapenv:Envelope>

您看到的异常是由于您尝试创建accountInfo节点的属性的方式。请注意,属性应作为参数传递给send方法:

xml.send("urn:accountInfo", "number"=>"#{number}", "secret"=>"#{secret}", "country"=>"US", "language"=>"en", "behalfOf"=>"#{behalfOf}")

可以在Envelope节点上获取属性:

xml.send("soapenv:Envelope", 'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/", 'xmlns:urn' => "urn:description7a.services.chrome.com")

最后,要获取评论,请使用comment方法:

xml.comment('You have a CHOICE of the next 3 items at this level')
xml.comment('Optional:')