PHP逆引き大全 516の極意

データフォーマットの極意 - Tips301 特定の要素の全ての属性情報を取得する

登録タグ
PHP
Icon comment count 0
Icon stock count 0

3-2XMLデータ操作(SimpleXML)Tips301 特定の要素の全ての属性情報を取得する

Level1

PointSimpleXMLElement::attributes()メソッドを利用する

特定の要素の全ての属性情報を取得するには、SimpleXMLElement::attributes()メソッドを使用します。

サンプルプログラムは、example.xmlの2番目の<book>タグ内の、<author>タグの属性値を取得しています。

リスト1301.php
<?php
header("Content-Type: text/html; charset=UTF-8");
echo "■メイン・サブの著者を取得する。<br />";
$xml = simplexml_load_file('example.xml');
$author_info = $xml->book[1]->author->attributes();
echo "<pre>";
print_r($author_info);
echo "</pre>";
?>

出典情報

Medium

PHP逆引き大全 516の極意

  • 著者: 大家 正登, 茂木 健一, 鮫島 康浩, 谷中 志織

コメント

    コメントはありません