我無法弄清楚如何從以下XML中解析「author」和「fact」標籤。如果格式對XML文檔看起來奇怪here is a link。使用Nokogiri解析XML
<response stat="ok">
−<ltml version="1.1">
−<item id="5403381" type="work">
<author id="21" authorcode="rowlingjk">J. K. Rowling</author>
<url>http://www.librarything.com/work/5403381</url>
−<commonknowledge>
−<fieldList>
−<field type="42" name="alternativetitles" displayName="Alternate titles">
−<versionList>
−<version id="3413291" archived="0" lang="eng">
<date timestamp="1298398701">Tue, 22 Feb 2011 13:18:21 -0500</date>
−<person id="18138">
<name>ablachly</name>
<url>http://www.librarything.com/profile/ablachly</url>
</person>
−<factList>
<fact>Harry Potter and the Sorcerer's Stone </fact>
</factList>
</version>
</versionList>
</field>
到目前爲止,我已經試過這個代碼來獲取作者,但它不工作:
@xml_doc = Nokogiri::XML(open("http://www.librarything.com/services/rest/1.1/?method=librarything.ck.getwork&isbn=0590353403&apikey=d231aa37c9b4f5d304a60a3d0ad1dad4"))
@xml_doc.xpath('//response').each do |n|
@author = n
end
謝謝邁克爾,工作很好 – Bryan 2011-05-06 17:23:31
沒問題!如果我的答案解決了您的問題,您可以點擊旁邊的複選標記將其標記爲「已接受」。 – michaelmichael 2011-05-06 18:09:51