PHP attributes() Function
Description
The attributes() function returns attributes and values within an XML tag.
Syntax
PHP attributes() Function has the following syntax.
attributes(ns,is_prefix);
Parameter
Parameter | Is Required | Description |
---|---|---|
ns | Optional. | A namespace for the retrieved attributes |
is_prefix | Optional. | A Boolean value. TRUE if ns is a prefix. FALSE if ns is a URI. Default is FALSE |
Return
Returns a SimpleXMLElement object on success.
Example
Return attributes and values within the XML body element:
<?php/*from w w w . j a va 2s . com*/
$note=<<<XML
<book>
<name date="2013-01-01" type="public">PHP</name>
<name date="2013-01-01" type="private">Java</name>
</book>
XML;
$xml=simplexml_load_string($note);
foreach($xml->body[0]->attributes() as $a => $b){
echo $a,'="',$b,"\"\n";
}
?>