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

 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. WampServer Version 2.5 bug修改

    做PHP开发都需要安装PHP的运行环境,为了方便,网上可以下载到好多的集成环境,最近使用WampServer Version 2.5发现有一些bug,分享一下修改的方法.高手请路过. 1.echo d ...

  2. [Android] Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'

    我们开发的产品,有一部分功能,需要在WebView中打开web页面,然后在web页面中通过js方法回调部分native的功能. 对于web回调native的开发方式,如果不了解的话,可以参考我以前的一 ...

  3. Android 系统ID介绍

    Android上系统ID有很多,本文只介绍常用的ANDROID ID.DEVICE ID.IMEI/MEID.WIFI/BT ADDRESS等几个,本文介绍这些ID的数据格式.长度及一些基本知识. 一 ...

  4. JQ二级菜单练习之一~~~

    <div class="nav"> <ul> <li><a href="#">首页</a> < ...

  5. 一张图让你看懂各开源License[转]

    你是否遇到过开源License,精炼而又晦涩的文字通常要读半天才能理解,而且大多数License差别不大,容易混淆.下面这张图让你段时间迅速掌握各种开源的License. 图片来源:阮一峰的博客. f ...

  6. Hibernate 映射关系

    映射组成关系 •建立域模型和关系数据模型有着不同的出发点: –域模型: 由程序代码组成, 通过细化持久化类的的粒度可提高代码的可重用性, 简化编程 –在没有数据冗余的情况下, 应该尽可能减少表的数目, ...

  7. python leetcode 日记--231. Power of Two

    题目: Given an integer, write a function to determine if it is a power of two. class Solution(object): ...

  8. hdu 2079

    ps:昨天刚做了个母函数的,觉得不太熟,今天又是母函数..很好.. 代码: #include "stdio.h" #include "string.h" ]; ...

  9. no package 'webkit-1.0' found

    linux安装程序的时候 ./configure 提示 no package 'webkit-1.0' found 解决方法: 安装 libwebkitgrk-dev包 1. sudo apt-get ...

  10. linux命令:which

    1.命令介绍: which用来在指定的路径搜索某个系统命令的位置,并返回第一个搜索结果. 2.命令格式: which [选项] 系统命令 3.命令参数: -n  指定文件名长度,指定的长度必须大于或等 ...