2009-07-02 65 views
1

我剛剛開始使用HTTParty,並且在從服務器回覆的XML中構建哈希的方式中遇到了問題。XML和HTTParty問題

如果我安裝在服務器上的以下工具模板:

xml.thunt :sendSubscriptionResult, :"xmlns:thunt" => "http://example.com/thunt", :status => @status 

一切正常,即由HTTParty建哈希通過生成器生成的XML,(後者可以通過使同樣的請求,可以觀察到匹配通過捲曲):

捲曲請求

curl -s -H "Accept: text/xml" -d "xml=`cat vendor/testxml/requests/sendsubscription.xml`" $SERVER/${name} 

REPL ÿ如由捲曲

'<thunt:sendSubscriptionResult xmlns:thunt="http://example.com/thunt" status="alreadySubscribed" />' 

HTTParty請求

TreasureHunt.post('/sendsubscription', :query => { :xml => sub }) 

回覆在HTTParty

{"thunt:sendSubscriptionResult"=>{"status"=>"alreadySubscribed", "xmlns:thunt"=>"http://example.com/thunt"}} 

但是看出,如果在Builder我指定我想要的sendSubscriptionResult元素有一個文本節點:

xml.thunt :sendSubscriptionResult, "Hello, World", :"xmlns:thunt" => "http://example.com/thunt", :status => @status 

(注意是「你好,世界」加法)這兩個工具突然不同意。

捲曲

'<thunt:sendSubscriptionResult xmlns:thunt="http://example.com/thunt" status="alreadySubscribed">Hello, World</thunt:sendSubscriptionResult>' 

HTTParty

{"thunt:sendSubscriptionResult"=>"Hello, World"} 

正如你所看到的,HTTParty已全部剝離元素的屬性,並已投入只有文本節點中所產生的哈希

這是HTTParty中的錯誤還是我做錯了什麼? 謝謝!

+0

我已經沒有什麼,但XML和HTTParty的麻煩,所以我很想看到這一個堅實的答案。 – tadman 2009-07-03 19:23:34

回答

0

我會繼續,並在問題頁面上發佈您的問題,爲他們的Github項目。

http://github.com/jnunemaker/httparty/issues

它已經看起來像有周邊的一些XML問題的一些問題。但它絕對是與開發人員直接溝通並向他們提供反饋的最佳方式。

+0

謝謝,看來我不是唯一一個注意到這個bug的人! – asymmetric 2009-07-05 10:42:36

0

在底層,httparty目前使用multi_xml進行XML解析。 multi_xml將使用基於可用解析gem的速度:Ox,LibXML,Nokogiri,REXML。也就是說,如果你安裝了它,它會首先選擇Ox。您也可以指定使用哪個解析器。

最近在multi_xml中解決了一些錯誤,特別是在數組方面。

我建議你點打捆到GitHub庫,以獲得最新的版本multi_xml的,在你的Gemfile這樣的:

gem 'multi_xml', :git => 'https://github.com/sferik/multi_xml' 
gem 'ox' 
gem 'httparty' 

然後,你要使用httparty(例如,在你的西納特拉服務器),你可以這樣做:

require 'bundler/setup' 

請注意,這個設置,multi_xml就不會在「寶石列表」輸出上顯示,但它會奏效。