如何使用 Nokogiri 解析 XML 并拆分节点值? [英] How do I parse XML using Nokogiri and split a node value?
本文介绍了如何使用 Nokogiri 解析 XML 并拆分节点值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 Nokogiri 来解析 XML.
I'm using Nokogiri to parse XML.
doc = Nokogiri::XML("http://www.enhancetv.com.au/tvguide/rss/melbournerss.php")
我不确定如何正确检索节点值.
I wasn't sure how to actually retrieve node values correctly.
我特别关注位于 item
下的 title
、link
和 description
节点父节点.
I'm after the title
, link
, and description
nodes in particular that sit under the item
parent nodes.
<item>
<title>Toasted TV - TEN - 07:00:00 - 21/12/2011</title>
<link>http://www.enhancetv.com.au/tvguide/</link>
<description>Join the team for the latest in gaming, sport, gadgets, pop culture, movies, music and other seriously fun stuff! Featuring a variety of your favourite cartoons.</description>
</item>
我想做的是 title.split("-")
,这样我就可以将日期和时间字符串转换为有效的 DateTime
对象稍后在轨道上使用.
What I'd like to do is title.split("-")
in such a way that I can convert the date and time strings into a valid DateTime
object to use later on down the track.
推荐答案
由于这是一个 RSS 提要,您可能需要考虑使用 RSS 解析器:
Since this is an RSS feed, you may want to consider an RSS parser:
require 'simple-rss'
require 'open-uri'
feed = 'http://www.enhancetv.com.au/tvguide/rss/melbournerss.php'
rss = SimpleRSS.parse open(feed)
rss.items.each do |item|
puts item.title, item.link, item.description
end
这篇关于如何使用 Nokogiri 解析 XML 并拆分节点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文