Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

書籍 Ruby Cookbook | Extracting Data from a Document's Tree Structure

パンくず

Ruby Cookbook
Extracting Data from a Document's Tree Structure

概要

Extracting Data from a Document's Tree Structure

内容

XML木構造のデータで取得するには
REXMLのインスタンスをeach_elementで取得する。

サンプルコード

# encoding: utf-8
require 'rexml/document'

HOGE_XML = %{
<hoge>
  <child_hoge>child_hoge</child_hoge>
  <child_hige>child_hige</child_hige>
  <child_hage>child_hage</child_hage>
</hoge>
}

hoges = REXML::Document.new(HOGE_XML)
hoges.root.each_element {|elm|puts elm}

出力

<child_hoge>child_hoge</child_hoge>
<child_hige>child_hige</child_hige>
<child_hage>child_hage</child_hage>