2010-10-15 14 views
5

मैं tuts का एक बहुत को देखा लेकिन this भी मुझे जहाँ मैंNokogiri XML पार्सिंग

नियंत्रक

def index 
     require 'nokogiri' 
     doc = Nokogiri::XML(open("http://sports.yahoo.com/top/rss.xml")) 

     @links = doc.xpath('//item').map do |i| 
     {'title' => i.xpath('title'), 'link' => i.xpath('link'), 'description' => i.xpath('description')} 
     end 
    end 

दृश्य

<ul> 
    <%= debug @links.each.first %> 
</ul> 

डिबग बाहर डाल

हूँ मिल गया है लगता है

{"title"=>[#<Nokogiri::XML::Element:0x8199ce34 name="title" children=[#<Nokogiri::XML::Text:0x8199c6f0 "Kolb to get start for Eagles vs. Falcons (AP)">]>], "description"=>[#<Nokogiri::XML::Element:0x8199b660 name="description" children=[#<Nokogiri::XML::Text:0x8199a594 "Kevin Kolb will make his second straight start in place of the injured Michael Vick when the Philadelphia Eagles host Atlanta on Sunday. Eagles coach Andy Reid says Vick practiced Friday for the first time since sustaining a rib cartilage injury on Oct. 3. There's a chance Vick will be the backup quarterback against his former team.">]>], "link"=>[#<Nokogiri::XML::Element:0x81999f40 name="link" children=[#<Nokogiri::XML::Text:0x81999b58 "http://us.rd.yahoo.com/sports/rss/top/SIG=11npql9k5/*http%3A//sports.yahoo.com/nfl/news?slug=ap-eagles-qbs">]>]}

मुझे जो चाहिए वह लिंक सरणी पर लूप करना और शीर्षक और लिंक के साथ हैश तक पहुंचना है, लेकिन मुझे नहीं पता कि यह कैसे करना है।

उत्तर

11

यदि मैं सही ढंग से समझ रहा हूं, तो आपको केवल xpath नोड्स से आंतरिक पाठ प्राप्त करने की आवश्यकता है।

{'title' => i.xpath('title').inner_text, 
'link' => i.xpath('link').inner_text, 
'description' => i.xpath('description').inner_text 
} 

.......

+0

:) - कि यह – s84

+0

'inner_text' के लिए धन्यवाद किया गया था;) –

संबंधित मुद्दे