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

 require 'nokogiri'

 xml_data=<<XML
<library>
<NAME><![CDATA[Favorite Books]]></NAME>
<book ISBN="">
<title>To Kill A Mockingbird</title>
<description><![CDATA[Description#1]]></description>
<author>Harper Lee</author>
</book>
<book ISBN="">
<title>Catcher in the Rye</title>
<description><![CDATA[This is an extremely intense description.]]></description>
<author>J. D. Salinger</author>
</book>
<book ISBN="">
<title>Murphy\'s Gambit</title>
<description><![CDATA[Daughter finds her dad!]]></description>
<author>Syne Mitchell</author>
</book>
</library>
XML # 载入数据
doc = Nokogiri::XML(xml_data) # 使用css获取到结点,遍历读取到Book对象中
doc.css('book').each do |node|
children = node.children Book.create(
:isbn => node['ISBN'],
:title => children.css('title').inner_text,
:description => children.css('description').inner_text,
:author => children.css('author').inner_text
)
end

nokogiri如何使用的更多相关文章

  1. mac gem install nokogiri error

    Gem::Ext::BuildError: ERROR: Failed to build gem native extension. /Users/angela/.rbenv/versions/1.9 ...

  2. Ruby:Nokogiri

    阅读推荐: Nokogiri的用法我推荐三篇非常给力的文章: http://ruby.bastardsbook.com/chapters/html-parsing/ http://ruby.basta ...

  3. Nokogiri爬虫教程

    Parsing HTML with Nokogiri http://ruby.bastardsbook.com/chapters/html-parsing/ Inspecting a Webpage' ...

  4. [翻译][Nokogiri官方教程] 解析HTML/XML文档 / Parsing an HTML/XML Document

    From a String From a File From the Internet Parse Options Encoding 原文: Parsing an HTML/XML Document ...

  5. nokogiri

    Nokogiri的用法我推荐三篇非常给力的文章:http://ruby.bastardsbook.com/chapters/html-parsing/http://ruby.bastardsbook. ...

  6. Ruby nokogiri 解析xml的简单实例

    require 'nokogiri'XML_FILE = "C:\\Users\\chenpassion\\Desktop\\20130806.xml"xml = Nokogiri ...

  7. Ruby爬虫header发送cookie,nokogiri解析html数据

    之前用php写过一个爬虫,同样是获取局域网的网站数据,这次我使用相同的网络环境,更低的电脑配置,使用ruby来再次爬虫,惊人的发现ruby使用自带的类库net/http爬取速度要远远超过php的cur ...

  8. nokogiri Fail install on Ruby 2.3 for Windows #1456 <From github>

    Q: gem install railson nokogiri install fail with error: 'nokogiri requires Ruby version < 2.3, & ...

  9. CocoaPod 使用方法

    huangyichengdeMacBook-Pro:~ Jack$ pod search AFNetworking/Library/Ruby/Site/2.0.0/rubygems.rb:250:in ...

随机推荐

  1. postgresql中执行计划

    1.Explain explain select * from tablename; 2.explain输出josn格式 explain (format json) select * from tab ...

  2. WPF CAL 计算器

    界面最终结果: 下载地址:https://skydrive.live.com/redir?resid=25C3908AA2038BDB!148&authkey=!ADR71XdB04LipYE

  3. mongodb unclean shutdown 修复方法

    启动mongodb时,提示Unclean shutdown detected mongodb,解决方法很简单 mongod --repair --dbpath D:\MongoDB\data\db

  4. 读javascript高级程序设计09-BOM

    一.window 1.在全局作用域中定义的变量和函数会被归在window对象. var a=1,b=2; function add(a,b){ return a+b; } console.log(wi ...

  5. 2014-07-29 Asp.Net 中级工程师 笔试题

    一.选择题    1.下列描述错误的是() A  类不可以被多重继承而接口可以: B  抽象类自身可以定义成员而接口不可以: C  抽象类和接口都不能被实例化: D   一个类可以继承多个基类和多个基 ...

  6. JavaWeb chapter 2 Servlet

    1.  什么是Servlet: Servlet是运行于Web容器中,按照其自身规范编写的Java应用程序. Servlet是用Java语言编写的,它是一个Java类,因而Servlet遵守所有Java ...

  7. PHP 面向对象编程

    面向对象——类: 创建一个类: //创建了一个没有内容的Person(人)类 class Person{ } //通过new关键字来 实例化一个类 $teacher = new Person; //t ...

  8. linux shell 指令 诸如-d, -f, -e之类的判断表达式

    文件比较运算符-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]-d filename 如果 filename为目录,则为真 [ -d /tmp/ ...

  9. PHP 配置文件中open_basedir选项作用

    如下是php.ini中的原文说明以及默认配置: ; open_basedir, if set, limits all file operations to the defined directory  ...

  10. a few changes of Android 5.0

    1.Service Intent must be explicit Intent serviceIntent = new Intent(context,MyService.class);context ...