2013-12-11 29 views
0

我需要建立以下XML数据问题在构建XML在轨使用引入nokogiri 4

<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"> 
+0

错误直接指出问题所在。第27行..与其下面的类似行相比,该行看起来有什么不同? :) –

+0

'(“number”=>“#{number}”“secret”=>“#{secret}”“country”=>“US”“language”=>“en”“representativeOf”=> {代表_Of}“)' 这是行。我的疑问是,如何'build such xml – Aravind

+0

http://nokogiri.org /Nokogiri/XML/Builder.html –

回答

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}") 

同样可以做得到信封节点上的属性:

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:')