SimpleXMLElement->attributes

(no version information, might be only in CVS)

SimpleXMLElement->attributes --  Identifies an element's attributes

Description

SimpleXMLElement simplexml_element->attributes ( [string data] )

This function provides the attributes and values defined within an xml tag.

注: SimpleXML 创建了一条给大部分方法增加交互式属性的规则。这些属性不能够使用 var_dump() 或其它检查对象的方法来查看。

例子 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";
}
?>

This script will display:

name="one"
game="lonely"


add a note add a note User Contributed Notes
skerr at mojavi dot org
10-Dec-2004 02:55
You can also access the node as an array to get attributes:

<?php

$xml
= simplexml_load_file('file.xml');

echo
'Attribute: ' . $xml['attribute'];

?>
christian at koch dot net
09-Nov-2004 04:35
PHP5:: I think this is a fine solution to get the attributes easier...

<?php

class EFXMLElement extends SimpleXMLElement {

   function
attributes() {
       return new
EFXMLAttributes(parent::attributes());
   }

}

class
EFXMLAttributes {

  
private $attributes;

   function
__construct($attributes) {
      
$this->attributes = $attributes;
   }

   function
__get($name) {
       if (isset(
$this->attributes[$name])) {
           return
$this->attributes[$name];
       } else {
           return
false;
       }
   }

}

if (
file_exists('pref.xml')) {
  
$tst = new EFXMLElement(file_get_contents('pref.xml'));
  
var_dump($tst->plugins->plugin[0]->attributes()->name);
} else {
   exit(
'Failed to open test.xml.');
}

?>
XML::example

<?xml version="1.0" encoding="ISO8859-1" ?>
<preferences>
   <system>
       EF3
   </system>
   <websites>
       <website name="demo">
           <scriptpath>/</scriptpath>
       </website>
       <website name="other">
           x   
       </website>
   </websites>
   <plugins>
       <plugin type="single" name="content" ontemplate="yes">
           <tag><![CDATA[<%content%>]]></tag>
           <class>EFContentPlugin</class>
           <source>/ef3/plugins/EFContentPlugin.class</source>
       </plugin>
       <plugin type="single" name="attribute" ontemplate="no">
           <tag><![CDATA[<%attribute @%>]]></tag>
           <class>EFAttributePlugin</class>
           <source>/ef3/plugins/EFAttributePlugin.class</source>
       </plugin>
       <plugin type="single" name="component" ontemplate="no">
           <tag><![CDATA[<%component @%>]]></tag>
           <class>EFComponentPlugin</class>
           <source>/ef3/plugins/EFComponentPlugin.class</source>
       </plugin>
       <plugin type="double" name="klammer" ontemplate="no">
           <tag><![CDATA[<KLAMMER@>]]></tag>
           <closer><![CDATA[<KLAMMER@>]]></closer>
           <class>KlammerPlugin</class>
           <source>/ef3/plugins/KlammerPlugin.class</source>
       </plugin>
   </plugins>
</preferences>
sveta at microbecal dot com
30-Oct-2004 06:03
Namespace handling example:
------------------------------------
test.xml:
<?xml version="1.0"?>
<Workbook xmlns="http://url1" xmlns:ss="http://url2">
 <Worksheet ss:Name="English">
 </Worksheet>
</Workbook>
script.php:
$xml_file = 'test.xml';
$xml = simplexml_load_file($xml_file);
foreach($xml->Worksheet->attributes('http://url2')
as $a => $b) {
   echo $a,'="',$b,"\"\n";
}
tychay at php dot net
29-Sep-2004 01:04
BTW, It occurs to me that why do you need the function anyway? You just access it directly as (string) $object[$attribute].
tychay at php dot net
29-Sep-2004 07:31
Why bother iterating over all the elements when they are already indexed? something like...

function simplexml_find_attribute(SimpleXMLElement $element, $attributeName) {
   $attrs = $element->attributes();
   if (isset($attrs[$attributeName])) {
       return (string) $attrs[$attributeName];
   }
   return false;
}

- terry
Eirik Sletteberg
12-Sep-2004 09:21
In reply to inge at elektronaut dot no's function:
Notice that this function returns an object, and not
a string. This is quite hard to notice at first, because
if you validate the return against a string, it will return
true:
print findAttribute($Fox, "foo") == "bar"? "Matches.":"Won't match."; // "Matches."

But, if you attempt to use the return for indexing in an array, like:
$MyArray[findattribute($Fox, "foo")] = "bar";

You will get an illegal offset type-error.
------
To correct this, we force-cast the return as a string:

function findAttribute($object, $attribute) {
  foreach($object->attributes() as $a => $b) {
   if ($a == $attribute) {
     $return = $b;
   }
  }
  if($return) {
   return (string)$return;
  }
}
inge at elektronaut dot no
27-May-2004 01:53
here's a simple function to get an attribute by name, based on the example

<?php
function findAttribute($object, $attribute) {
  foreach(
$object->attributes() as $a => $b) {
   if (
$a == $attribute) {
    
$return = $b;
   }
  }
  if(
$return) {
   return
$return;
  }
}
?>