PHP attributes() Function

Definition and Usage

The attributes() function retrieves the attributes of a SimpleXML element.

This function provides attributes and values defined within an XML tag.

Syntax

class SimpleXMLElement
{
string attributes(ns,is_prefix)
}
Parameter Description
ns Optional. The namespace of the attribute to be searched.
is_prefix Optional. The default is false.

Example

XML File:

<?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 Code:

<?php
$xml = simplexml_load_file("test.xml");
foreach($xml->body[0]->attributes() as $a => $b)
  {
  echo $a, '="', $b, '"';
  }
?>

Output:

type="small" important="low"