savon响应无法转换为正确的xml

时间:2014-10-09 07:45:06

标签: ruby-on-rails ruby wsdl nokogiri savon

我收到了Savon(wsdl webservice)客户端的响应,

:departure_airport=>{:@location_code=>"MAA", :@code_context=>"IATA"}

当我尝试转换response.to_xml时,我正在

预期输出:

我希望这个响应适当的xml,如果@表示,它应该是属性,否则为innexText

<departure_airport location_code="MAA" code_context="IATA" />

实际输出:

<departure_airport>
   <location_code>MAA</location_code>
   <code_context>IATA</code_context>
 </departure_airport>

帮助我获得实际的回复。我正在使用Nokogiri来解析XML响应。


完整回复:

[{: flight_segment => {: departure_airport => {: @location_code => "MAA", : @code_context => "IATA"
        }, : arrival_airport => {: @location_code => "DEL", : @code_context => "IATA"
        }, : operating_airline => "JetLite India", : equipment => "73G", : marketing_airline => "JetLite India", : requested_cabin => {: @cabin_type => "Economy"
        }, : booking_class_avails => {: booking_class_avail => {: @res_book_desig_code => "O", : @res_book_desig_quantity => "9"
            }, : @cabin_type => "Economy"
        }, : stop_location => {: @location_code => "BLR", : @code_context => "iata"
        }, : @departure_date_time => "2014-10-10T08:20:00", : @arrival_date_time => "2014-10-10T12:30:00", : @stop_quantity => "1", : @flight_number => "4607", : @res_book_desig_code => "O", : @departure_day => "Fri"
    }, : booking_reference_id => {: company_name => "Amadeus", : @type => "5", : @id => "1", : @id_context => "1A", : @sell_reference => "CPTS128TS", : @ticket_reference => "CPTS12801"
    }, : @ts_duration => "15000000", : @ref_number => "1"
}, {: flight_segment => {: departure_airport => {: @location_code => "DEL", : @code_context => "IATA"
        }, : arrival_airport => {: @location_code => "MAA", : @code_context => "IATA"
        }, : operating_airline => "Jet Airways", : equipment => "73G", : marketing_airline => "Jet Airways", : requested_cabin => {: @cabin_type => "Economy"
        }, : booking_class_avails => {: booking_class_avail => {: @res_book_desig_code => "O", : @res_book_desig_quantity => "9"
            }, : @cabin_type => "Economy"
        }, : stop_location => {: @location_code => "BLR", : @code_context => "iata"
        }, : @departure_date_time => "2014-10-10T17:00:00", : @arrival_date_time => "2014-10-10T21:10:00", : @stop_quantity => "1", : @flight_number => "7075", : @res_book_desig_code => "O", : @departure_day => "Fri"
    }, : booking_reference_id => {: company_name => "Amadeus", : @type => "5", : @id => "2", : @id_context => "1A", : @sell_reference => "CPTS128TS", : @ticket_reference => "CPTS12801"
    }, : @ts_duration => "15000000", : @ref_number => "2"
}]

1 个答案:

答案 0 :(得分:0)

冒号(:)与完整响应中的哈希键之间有一个空格。我不确定它是否在复制时出现,但当我删除该空间并尝试:

   json_data = [{:flight_segment=>{:departure_airport=>{:@location_code=>"MAA", :@code_context=>"IATA"}, :arrival_airport=>{:@location_code=>"DEL", :@code_context=>"IATA"}, :operating_airline=>"JetLite India", :equipment=>"73G", :marketing_airline=>"JetLite India", :requested_cabin=>{:@cabin_type=>"Economy"}, :booking_class_avails=>{:booking_class_avail=>{:@res_book_desig_code=>"O", :@res_book_desig_quantity=>"9"}, :@cabin_type=>"Economy"}, :stop_location=>{:@location_code=>"BLR", :@code_context=>"iata"}, :@departure_date_time=>"2014-10-10T08:20:00", :@arrival_date_time=>"2014-10-10T12:30:00", :@stop_quantity=>"1", :@flight_number=>"4607", :@res_book_desig_code=>"O", :@departure_day=>"Fri"}, :booking_reference_id=>{:company_name=>"Amadeus", :@type=>"5", :@id=>"1", :@id_context=>"1A", :@sell_reference=>"CPTS128TS", :@ticket_reference=>"CPTS12801"}, :@ts_duration=>"15000000", :@ref_number=>"1"}]

json_data.to_xml效果很好。

  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<objects type=\"array\">\n  <object>\n    <flight-segment>\n      <departure-airport>\n        <@location-code>MAA</@location-code>\n        <@code-context>IATA</@code-context>\n      </departure-airport>\n      <arrival-airport>\n        <@location-code>DEL</@location-code>\n        <@code-context>IATA</@code-context>\n      </arrival-airport>\n      <operating-airline>JetLite India</operating-airline>\n      <equipment>73G</equipment>\n      <marketing-airline>JetLite India</marketing-airline>\n      <requested-cabin>\n        <@cabin-type>Economy</@cabin-type>\n      </requested-cabin>\n      <booking-class-avails>\n        <booking-class-avail>\n          <@res-book-desig-code>O</@res-book-desig-code>\n          <@res-book-desig-quantity>9</@res-book-desig-quantity>\n        </booking-class-avail>\n        <@cabin-type>Economy</@cabin-type>\n      </booking-class-avails>\n      <stop-location>\n        <@location-code>BLR</@location-code>\n        <@code-context>iata</@code-context>\n      </stop-location>\n      <@departure-date-time>2014-10-10T08:20:00</@departure-date-time>\n      <@arrival-date-time>2014-10-10T12:30:00</@arrival-date-time>\n      <@stop-quantity>1</@stop-quantity>\n      <@flight-number>4607</@flight-number>\n      <@res-book-desig-code>O</@res-book-desig-code>\n      <@departure-day>Fri</@departure-day>\n    </flight-segment>\n    <booking-reference-id>\n      <company-name>Amadeus</company-name>\n      <@type>5</@type>\n      <@id>1</@id>\n      <@id-context>1A</@id-context>\n      <@sell-reference>CPTS128TS</@sell-reference>\n      <@ticket-reference>CPTS12801</@ticket-reference>\n    </booking-reference-id>\n    <@ts-duration>15000000</@ts-duration>\n    <@ref-number>1</@ref-number>\n  </object>\n</objects>\n"