2012-04-12 21 views

答えて

4
require 'rubygems' 
require 'nokogiri' 

string = %Q{ 
    <?xml version="1.0" encoding="UTF-8"?> 
    <response status="ok" permission_level="admin" message="ready to use" cached="0"> 
    <title>kit</title> 
    </response> 
} 

doc = Nokogiri::XML(string) 
doc.css("response").each do |response_node| 
    puts response_node["message"] 
end 

このルビーのファイルを保存して実行し、結果を取得します:

#=> ready to use 
+0

%のQは、それが必要ない、とは何ですか? –

+0

もちろん、%Q {}は複数行の文字列を定義するために使用されます。 –

+1

いいえ、それは「必要」ではありません。また、単に単一引用符、または[ヒアドキュメント](http://log.gmarik.info/2007/12/rubys-here-document-heredoc-mini.html)を使用することができます。 – Phrogz

0

あなたがそれらを添字。

doc = Nokogiri::HTML(open('http://google.com')) 
doc.css('img:first').first['alt'] 
=> "Google" 
関連する問題