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>Reminder</heading> <body type="small" important="low">Don't forget the meeting!</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"