PHP attributes() Function

In this chapter you will learn:

  1. Description for PHP attributes() Function
  2. Syntax for PHP attributes() Function
  3. Parameter for PHP attributes() Function
  4. Return for PHP attributes() Function
  5. Example - Return attributes and values within the XML body element

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

ParameterIs RequiredDescription
nsOptional.A namespace for the retrieved attributes
is_prefixOptional.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 java  2  s. 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";
}
?>

Next chapter...

What you will learn in the next chapter:

  1. Description for PHP children() Function
  2. Syntax for PHP children() Function
  3. Parameter for PHP children() Function
  4. Return for PHP children() Function
  5. Example - Find the children of the note node
Home » PHP Tutorial » PHP SimpleXML Functions
PHP SimpleXMLElement Create
PHP addAttribute() Function
PHP addChild() Function
PHP asXML() Function
PHP attributes() Function
PHP children() Function
PHP SimpleXML count() Function
PHP getDocNamespaces() Function
PHP getName() Function
PHP getNamespaces() Function
PHP registerXPathNamespace() Function
PHP simplexml_import_dom() Function
PHP simplexml_load_file() Function
PHP simplexml_load_string() Function
PHP xpath() Function