Php/docs/simplexmlelement.attributes
SimpleXMLElement::attributes
(PHP 5, PHP 7)
SimpleXMLElement::attributes — Identifies an element's attributes
说明
public SimpleXMLElement::attributes
([ string $ns
= null
[, bool $is_prefix
= false
]] ) : SimpleXMLElement
This function provides the attributes and values defined within an xml tag.
Note:
SimpleXML建起了一个给大多数方法添加迭代属性的规则。不能通过使用 var_dump() 或任何可检查对象的其它东西来查看。
参数
ns
- An optional namespace for the retrieved attributes
is_prefix
- Default to
false
返回值
Returns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.
Returns null
if called on a SimpleXMLElement
object that already represents an attribute and not a tag.
更新日志
版本 | 说明 |
---|---|
5.2.0 | The optional parameter is_prefix was added.
|
范例
Example #1 Interpret an XML string
<?php$string = <<<XML<a> <foo name="one" game="lonely">1</foo></a>XML;$xml = simplexml_load_string($string);foreach($xml->foo[0]->attributes() as $a => $b) { echo $a,'="',$b,"\"\n";}?>
以上例程会输出:
name="one" game="lonely"