ホーム > ブログ > Ruby LibXML-Ruby

Ruby LibXML-Ruby

LibXML-Rubyはgemでインストールできます。
# gem install libxml-ruby

※LibXML-Rubyのインストールには、以下のライブラリが必要です。
インストールされていない場合は、別途インストールします。
libm
libz
libiconv
libxml2

簡単に使い方を紹介します。
require 'rubygems'
require 'xml/libxml'

str=<<EOS
<?xml version="1.0" encoding="UTF-8" ?>
<wine>
  <item>
    <rank>1</rank>
    <country>フランス</country>
    <amount>5,329,449</amount>
  </item>
  <item>
    <rank>2</rank>
    <country>イタリア</country>
    <amount>5,056,648</amount>
  </item>
  <item>
    <rank>3</rank>
    <country>スペイン</country>
    <amount>3,934,140</amount>
  </item>
</wine>
EOS

doc = XML::Document.string(str) 

# <country>の要素を取り出してみます。
doc.root.find('/wine/iem/country').each do |country|
  puts country.content
end

出力結果:
フランス
イタリア
スペイン

LibXML-Rubyの使い方は下記ブログが参考になります。
LibXML-Rubyメモ。 – マルニカ。

詳細はrdocをご欄下さい。
LibXML Ruby
前の記事 «
次の記事 »