downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

SimpleXMLElement::children> <SimpleXMLElement::asXML
Last updated: Fri, 20 May 2011

view this page in

SimpleXMLElement::attributes

(PHP 5 >= 5.0.1)

SimpleXMLElement::attributesIdentifica el atributo de un elemento

Descripción

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

Esta función provee de los atributos y valores definidos dentro de una etiqueta XML.

Note: SimpleXML ha desarrollado una regla para el añadido de propiedades iterativas a la mayoría de métodos. No pueden ser examinados usando var_dump() ni con cualquier otra función que examine objetos.

Parámetros

ns

Un namespace opcional para los atributos recuperados

is_prefix

Por defecto FALSE

Valores devueltos

Devuelve un objeto SimpleXMLElement que puede ser iterado a través de los atributos en la etiqueta.

Devuelve NULL si se invoca sobre un objeto SimpleXMLElement que ya representa un atributo y no una etiqueta.

Ejemplos

Example #1 Interpreta un string XML

<?php
$string 
= <<<XML
<a>
 <prueba nombre="uno" juego="solitario">1</prueba>
</a>
XML;

$xml simplexml_load_string($string);
foreach(
$xml->prueba[0]->attributes() as $a => $b) {
    echo 
$a,'="',$b,"\"\n";
}
?>

El resultado del ejemplo sería:

nombre="uno"
juego="solitario"



SimpleXMLElement::children> <SimpleXMLElement::asXML
Last updated: Fri, 20 May 2011
 
add a note add a note User Contributed Notes SimpleXMLElement::attributes
Andrei 16-May-2011 11:46
Reading the attributes of the root element with name space prefixes as in an Atom feed

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/&quot;Ck4EQXYzeCp7ImA9WhZWFE4.&quot;">

<?php
$xml
=  @simplexml_load_file($feed);
$att_gd = $xml->attributes("gd",1);
$Etag = $att_gd["etag"];
?>
sarlak 27-Apr-2011 08:12
<?php
$att
= 'attribueName';

// You can access an element's attribute just like this :
$attribute = $element->attributes()->$att;

// This will save the value of the attribute, and not the objet
$attribute = (string)$element->attributes()->$att;

// You also can edit it this way :
$element->attributes()->$att = 'New value of the attribute';
?>
leap 02-Apr-2011 05:07
If you want to save the value of an attribute into an array, typecast it to a string.

<?php
// stores an xml-element-object
$dataStore['value'] = $attributes->myValue

// stores the value of the attribute
$dataStore['value'] = (string)$attributes->myValue
?>
alan at performantsystems dot com 24-May-2010 05: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-Apr-2010 02: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 04-Nov-2009 10:21
To get an attribute in the node, use node->attributes()->attributeName
skerr at mojavi dot org 10-Dec-2004 06: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-May-2004 05: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;
  }
}
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites