PHP attributes() 関数

定義と使用方法

attributes() 関数は、SimpleXML 要素の属性を取得します。

この関数は、XML タグで定義された属性と値を提供します。

構文

class SimpleXMLElement
{
string attributes(ns,is_prefix)
}
引数 説明
ns オプションです。検索される属性の命名空間。
is_prefix オプションです。デフォルトは false です。

XML ファイル:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>リマインダー</heading>
<body type="small" important="low">会議を忘れないように!</body>
</note>

PHP コード:

<?php
$xml = simplexml_load_file("test.xml");
foreach($xml->body[0]->attributes() as $a => $b)
  {
  echo $a,'="',$b,'"';
  }
?>

出力:

type="small" important="low"