PHPattributes()函数
定义和用法
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"