nokogiri如何使用

來源:互聯網
上載者:User

標籤:

直接來個簡單的代碼執行個體就明白啦!

 1 require ‘nokogiri‘ 2  3 xml_data=<<XML 4   <library> 5     <NAME><![CDATA[Favorite Books]]></NAME> 6     <book ISBN="11342343"> 7       <title>To Kill A Mockingbird</title> 8       <description><![CDATA[Description#1]]></description> 9       <author>Harper Lee</author>10     </book>11     <book ISBN="989894781234">12       <title>Catcher in the Rye</title>13       <description><![CDATA[This is an extremely intense description.]]></description>14       <author>J. D. Salinger</author>15     </book>16     <book ISBN="123456789">17       <title>Murphy\‘s Gambit</title>18       <description><![CDATA[Daughter finds her dad!]]></description>19       <author>Syne Mitchell</author>20     </book>21   </library>22 XML23 24 # 載入資料25 doc = Nokogiri::XML(xml_data)26 27 # 使用css擷取到結點,遍曆讀取到Book對象中28 doc.css(‘book‘).each do |node|29   children = node.children30 31   Book.create(32     :isbn => node[‘ISBN‘],33     :title => children.css(‘title‘).inner_text,34     :description => children.css(‘description‘).inner_text,35     :author => children.css(‘author‘).inner_text36   )37 end

 

nokogiri如何使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.