PHP attributes() 函數(shù)
定義和用法
attributes() 函數(shù)獲取 SimpleXML 元素的屬性。
該函數(shù)提供在一個(gè) XML 標(biāo)簽中定義的屬性和值。
語(yǔ)法
class SimpleXMLElement
{
string attributes(ns,is_prefix)
}
| 參數(shù) | 描述 |
|---|---|
| ns | 可選。被檢索的屬性的命名空間。 |
| is_prefix | 可選。默認(rèn)是 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"