這是我第一次嘗試從API獲取數據,並將數據輸出到視圖。API和XML數據軌道3 Nokogiri
我想將一個ISBN號碼放入搜索表單中,並使用http://isbndb.com/獲取該特定書籍的數據。這是我到目前爲止有:
控制器:
require 'open-uri'
class BookController < ApplicationController
def searchbook
resp = open("http://isbndb.com/api/books.xml?access_key=#{'API KEY HERE'}&results=texts&index1=isbn&value1=#{params[:isbn]}")
doc = Nokogiri.XML(resp.read)
# ... process response here
end
end
形式:
<%= form_tag({:controller => 'book', :action => 'searchbook'}, {:method => 'get'}) do |select| %>
<%= label_tag :isbn, "Enter ISBN Number" %>
<%= text_field_tag :isbn, params[:isbn] %>
<%= submit_tag "Search" %>
<% end %>
的XML要返回
<?xml version="1.0" encoding="UTF-8"?>
<ISBNdb server_time="2005-07-29T03:02:22">
<BookList total_results="1" page_size="10" page_number="1" shown_results="1">
<BookData book_id="paul_laurence_dunbar" isbn="0766013502">
<Title>Paul Laurence Dunbar</Title>
<TitleLong>Paul Laurence Dunbar: portrait of a poet</TitleLong>
<AuthorsText>Catherine Reef</AuthorsText>
<PublisherText publisher_id="enslow_publishers">Berkeley Heights, NJ: Enslow Publishers, c2000.</PublisherText>
<Summary>A biography of the poet who faced racism and devoted himself to depicting the black experience in America.</Summary>
<Notes>"Works by Paul Laurence Dunbar": p. 113-114. Includes bibliographical references (p. 124) and index.</Notes>
<UrlsText></UrlsText>
<AwardsText></AwardsText>
</BookData>
</BookList>
</ISBNdb>
如何處理XML請求或我能讀什麼來了解如何?
我在哪裏可以查看控制檯中返回的數據(如果有)?我甚至不確定這是否還在做任何事情,但是通過點擊我的表單中的「搜索」我會將其帶到現在是空白頁面的搜索簿操作。
我可能還有很長的路要走,但這是我第一次這樣做。
沒有你試圖解析它很難爲您提供的示例代碼使用XML的一個樣本。使用Nokogiri解析XML很容易,但是,如果沒有示例XML,您需要使用[Nokogiri的解析教程](http://nokogiri.org/tutorials/parsing_an_html_xml_document.html)。 –
我已經添加了預期的XML,這是否有幫助 – Richlewis