我需要帮助从ruby / rails中的xml中提取值

时间:2012-10-09 04:05:15

标签: ruby-on-rails ruby xml nokogiri

我正在开发个人项目并使用Rails来学习框架。该项目是基于音乐的,我正在使用ChartLyrics.com的API来检索歌词。 API返回XML,我无法从XML中提取实际的lyric元素。

我已经安装了Nokogiri gem来帮助解析XML。以下是我用来检索数据的内容。从rails控制台:

doc = Nokogiri::XML(open(http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=michael%20jackson&song=bad))
puts doc

<?xml version="1.0" encoding="utf-8"?>
<GetLyricResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://api.chartlyrics.com/">
  <TrackId>0</TrackId>
  <LyricChecksum>a4a56a99ee00cd8e67872a7764d6f9c6</LyricChecksum>
  <LyricId>1710</LyricId>
  <LyricSong>Bad</LyricSong>
  <LyricArtist>Michael Jackson</LyricArtist>
  <LyricUrl>http://www.chartlyrics.com/28h-8gWvNk-Rbj1X-R7PXg/Bad.aspx</LyricUrl>
  <LyricCovertArtUrl>http://ec1.images-amazon.com/images/P/B000CNET66.02.MZZZZZZZ.jpg</LyricCovertArtUrl>
  <LyricRank>9</LyricRank>
  <LyricCorrectUrl>http://www.chartlyrics.com/app/correct.aspx?lid=MQA3ADEAMAA=</LyricCorrectUrl>
  <Lyric>
     Because I'm bad (bad-bad), I'm bad, come on (really, really bad)
     You know I'm bad (bad-bad), I'm bad, you know it (really, really bad)
     You know I'm bad (bad-bad), I'm bad, you know it (really, really bad) you know
     And the whole world has to answer right now
     Just to tell you once again
  </Lyric>
</GetLyricResult>

我缩短了歌词以节省空间。如何提取'Lyric'元素?我已经尝试了以下所有方法:

> lyrics = doc.xpath('//Lyric')
=> []

> lyrics = doc.xpath('/Lyric')
=> []

> lyrics = doc.xpath('//GetLyricResult/Lyric')
=> []

> lyrics = doc.xpath('//GetLyricResult//Lyric')
=> []

> lyrics = doc.xpath('/GetLyricResult/Lyric')
=> []

'lyrics'每次都是零。谁能告诉我我做错了什么?感谢

1 个答案:

答案 0 :(得分:3)

默认情况下,nokogiri会查找不在任何命名空间中的元素,但此文档是命名空间:

doc.namespaces
#=> {"xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance", "xmlns:xsd"=>"http://www.w3.org/2001/XMLSchema", "xmlns"=>"http://api.chartlyrics.com/"}

因此,您必须将xmlns命名空间附加到您要搜索的代码中(您可以省略实际的网址,因为nokogiri will fill in the URL for the default namespace in for you):

doc.xpath('//xmlns:Lyric')

或者,您可以使用css进行搜索:

doc.css('Lyric')

另请参阅:Why doesn't Nokogiri xpath like xmlns declarations