PHP Doku:: Identifies an element s attributes - simplexmlelement.attributes.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationSimpleXMLThe SimpleXMLElement classSimpleXMLElement::attributes

Ein Service von Reinhard Neidl - Webprogrammierung.

The SimpleXMLElement class

<<SimpleXMLElement::asXML

SimpleXMLElement::children>>

SimpleXMLElement::attributes

(PHP 5 >= 5.0.1)

SimpleXMLElement::attributesIdentifies an element's attributes

Beschreibung

public SimpleXMLElement SimpleXMLElement::attributes ([ string $ns = NULL [, bool $is_prefix = false ]] )

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

Hinweis: SimpleXML definiert für die meisten Methoden Regeln für das Hinzufügen von iterativen Eigenschaften. Diese können weder mit var_dump() oder auf andere Weise angezeigt werden.

Parameter-Liste

ns

An optional namespace for the retrieved attributes

is_prefix

Default to FALSE

Rückgabewerte

Returns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.

Returns NULL if called on a SimpleXMLElement object that already represents an attribute and not a tag.

Beispiele

Beispiel #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";
}
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

name="one"
game="lonely"


5 BenutzerBeiträge:
- Beiträge aktualisieren...
alan at performantsystems dot com
24.05.2010 7:26
So lets say you have database type data in an XML string called $xmlstring with the key or item ID as an XML Attribute and all content data as regular XML Elements, as above. SimpleXML processes the Attributes as an array, so we can play along and push the Attributes into an array. Then we can get the value of any specific Attribute we want by addressing it by name, such as "ID".

Considering this data:

<?xml version="1.0" encoding="utf-8"?>
<data>
   <item ID="30001">
      <Company>Navarro Corp.</Company>
   </item>
   <item ID="30002">
      <Company>Performant Systems</Company>
   </item>
   <item ID="30003">
      <Company>Digital Showcase</Company>
   </item>
</data>

Example of listing both the ID Attribute and Company Element values:
 
<?php
$xmlObject
= new SimpleXMLElement($xmlstring);
foreach (
$xmlObject->children() as $node) {
       
$arr = $node->attributes();   // returns an array
       
print ("ID=".$arr["ID"]);     // get the value of this attribute
       
print ("  Company=".$node->Company);
        print (
"<p><hr>");
}
?>
Xeoncross
11.04.2010 4:46
It is really simple to access attributes using array form. However, you must convert them to strings or ints if you plan on passing the values to functions.

<?php
SimpleXMLElement Object
(
    [@
attributes] => Array
        (
            [
id] => 55555
       
)

    [
text] => "hello world"
)
?>

Then using a function

<?php
function xml_attribute($object, $attribute)
{
    if(isset(
$object[$attribute]))
        return (string)
$object[$attribute];
}
?>

I can get the "id" like this

<?php
print xml_attribute($xml, 'id'); //prints "55555"
?>
gillllberg at gmail dot com
4.11.2009 23:21
To get an attribute in the node, use node->attributes()->attributeName
skerr at mojavi dot org
10.12.2004 7:55
You can also access the node as an array to get attributes:

<?php

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

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

?>
inge at elektronaut dot no
26.05.2004 19: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;
  }
}
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)