Note that there are some explanatory texts on larger screens.

plurals
  1. POI need help extracting values from xml in ruby/rails
    text
    copied!<p>I am working on personal project and using Rails to learn the framework. The project is music based and I'm using ChartLyrics.com's API to retrieve song lyrics. The API returns XML and I am having trouble extracting the actual lyric element from the XML. </p> <p>I have installed the Nokogiri gem to help parse the XML. The following is what I'm using to retrieve the data. From the rails console:</p> <pre><code>doc = Nokogiri::XML(open(http://api.chartlyrics.com/apiv1.asmx/SearchLyricDirect?artist=michael%20jackson&amp;song=bad)) puts doc &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;GetLyricResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://api.chartlyrics.com/"&gt; &lt;TrackId&gt;0&lt;/TrackId&gt; &lt;LyricChecksum&gt;a4a56a99ee00cd8e67872a7764d6f9c6&lt;/LyricChecksum&gt; &lt;LyricId&gt;1710&lt;/LyricId&gt; &lt;LyricSong&gt;Bad&lt;/LyricSong&gt; &lt;LyricArtist&gt;Michael Jackson&lt;/LyricArtist&gt; &lt;LyricUrl&gt;http://www.chartlyrics.com/28h-8gWvNk-Rbj1X-R7PXg/Bad.aspx&lt;/LyricUrl&gt; &lt;LyricCovertArtUrl&gt;http://ec1.images-amazon.com/images/P/B000CNET66.02.MZZZZZZZ.jpg&lt;/LyricCovertArtUrl&gt; &lt;LyricRank&gt;9&lt;/LyricRank&gt; &lt;LyricCorrectUrl&gt;http://www.chartlyrics.com/app/correct.aspx?lid=MQA3ADEAMAA=&lt;/LyricCorrectUrl&gt; &lt;Lyric&gt; 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 &lt;/Lyric&gt; &lt;/GetLyricResult&gt; </code></pre> <p>I shortened the lyrics to save space. How do I extract the 'Lyric' element? I've tried all of the following:</p> <pre><code>&gt; lyrics = doc.xpath('//Lyric') =&gt; [] &gt; lyrics = doc.xpath('/Lyric') =&gt; [] &gt; lyrics = doc.xpath('//GetLyricResult/Lyric') =&gt; [] &gt; lyrics = doc.xpath('//GetLyricResult//Lyric') =&gt; [] &gt; lyrics = doc.xpath('/GetLyricResult/Lyric') =&gt; [] </code></pre> <p>'lyrics' is nil everytime. Can anyone tell me what I'm doing wrong? Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload