jruby での nokogiri 。チュートリアルの勝手なまとめ その3
さて、jruby での nokogiri チュートリアルの勝手なまとめ の第3回目です。
前にも書きましたように、jruby 1.5.6 は cruby1.8.7 とほぼ同等ですが、マルチバイト文字の扱いはまったく同じではありません。私は cruby1.8.7 での動作の検証はやってません。
nokogiri のバージョンは 1.4.4.2 です。
ubuntu10.04 を使用していますので、文字環境は utf-8 がデフォです。
名前空間について
url は一意であるので、それに対応付けられた
(世界で公開されている xml 上で名前が衝突しない、ということ)
いま、ホームディレクトリに parts.xml があるとして、
サンプル・コードを示す。
[parts.xml]
<parts>
<!-- Alice's Auto Parts Store -->
<inventory xmlns="http://alicesautoparts.com/">
<tire>all weather</tire>
<tire>studded</tire>
<tire>extra wide</tire>
</inventory> <!-- Bob's Bike Shop -->
<inventory xmlns="http://bobsbikes.com/">
<tire>street</tire>
<tire>mountain</tire>
</inventory>
</parts>
サンプル・コード
$KCODE = 'UTF8'require 'rubygems'
require 'nokogiri'
# カレントディレクトを HomeDirectory に指定
Dir.chdir(File.expand_path("~/"))
@doc = Nokogiri::XML(File.read("parts.xml"))
car_tires = @doc.xpath('//car:tire', 'car' => 'http://alicesautoparts.com/')
puts '■ car_tires の要素 を出力'
car_tires.each{|elem| puts elem}
bike_tires = @doc.xpath('//bike:tire', 'bike' => 'http://bobsbikes.com/')
puts '■ bike_tires の要素 を出力'
bike_tires.each{|elem| puts elem}
=begin
実行結果
■ car_tires の要素 を出力
<tire>all weather</tire>
<tire>studded</tire>
<tire>extra wide</tire>
■ bike_tires の要素 を出力
<tire>street</tire>
<tire>mountain</tire>
=end
命名規則に従うなら、コードをより短くできる。
例として、あるAtomフィードの処理を示す。
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"> <title>Example Feed</title>
<link href="http://example.org/"/>
<updated>2003-12-13T18:30:02Z</updated>
<author>
<name>John Doe</name>
</author>
<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
<entry>
<title>Atom-Powered Robots Run Amok</title>
<link href="http://example.org/2003/12/13/atom03"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<summary>Some text.</summary>
</entry>
</feed>
規約に従うなら、我々はこのようにすべてのタイトル・タグを取得することが出来る。
(atom.xml を ホームディレクトリに置く)
サンプル・コード
$KCODE = 'UTF8'require 'rubygems'
require 'nokogiri'
# カレントディレクトを HomeDirectory に指定
Dir.chdir(File.expand_path("~/"))
@doc = Nokogiri::XML(File.read("atom.xml"))
tag_titles = @doc.xpath('//xmlns:title')
tag_titles.each{|elem| puts elem}
=begin
実行結果
<title>Example Feed</title>
<title>Atom-Powered Robots Run Amok</title>
=end
名前空間の利点を得るには、XPathを使用しないで、
CSSセレクタを使用することも出来る。
CSSの使用例
$KCODE = 'UTF8'require 'rubygems'
require 'nokogiri'
# カレントディレクトを HomeDirectory に指定
Dir.chdir(File.expand_path("~/"))
@doc = Nokogiri::XML(File.read("atom.xml"))
tag_titles2 = @doc.css('xmlns|title')
tag_titles2.each{|elem| puts elem}
=begin
実行結果
<title>Example Feed</title>
<title>Atom-Powered Robots Run Amok</title>
=end
CSSを使用するとき、
名前空間が"xmlns"と呼ばれている場合、名前空間の名を省略出来る。
$KCODE = 'UTF8'require 'rubygems'
require 'nokogiri'
# カレントディレクトを HomeDirectory に指定
Dir.chdir(File.expand_path("~/"))
@doc = Nokogiri::XML(File.read("atom.xml"))
tag_titles3 = @doc.css('title')
tag_titles3.each{|elem| puts elem}
=begin
実行結果
<title>Example Feed</title>
<title>Atom-Powered Robots Run Amok</title>
=end
さて、この項、まだまだ続きます。