zoukankan      html  css  js  c++  java
  • nokogiri如何使用

    直接来个简单的代码实例就明白啦!

     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 XML
    23 
    24 # 载入数据
    25 doc = Nokogiri::XML(xml_data)
    26 
    27 # 使用css获取到结点,遍历读取到Book对象中
    28 doc.css('book').each do |node|
    29   children = node.children
    30 
    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_text
    36   )
    37 end
  • 相关阅读:
    ACdream 1069 无耻的出题人
    ACdream 1064 完美数
    ACdream 1028 Path
    ACdream 1020 The Game about KILL
    ACdream 1015 Double Kings
    CodeForces
    Codeforces 390A( 模拟题)
    Codeforces 389B(十字模拟)
    Codeforces 389A (最大公约数)
    Codeforces 417 C
  • 原文地址:https://www.cnblogs.com/angestudy/p/4422948.html
Copyright © 2011-2022 走看看