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

search for in the

DOMElement::getAttributeNode> <DOMElement::__construct
[edit] Last updated: Fri, 17 May 2013

view this page in

DOMElement::getAttribute

(PHP 5)

DOMElement::getAttributeDevuelve el valor de un atributo

Descripción

string DOMElement::getAttribute ( string $name )

Obtiene el valor del atributo de nombre name para el nodo actual.

Parámetros

name

El nombre del atributo.

Valores devueltos

El valor del atributo, o una cadena vacía si no se encuentra un atributo con el nombre dado por name.

Ver también



DOMElement::getAttributeNode> <DOMElement::__construct
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes DOMElement::getAttribute - [3 notes]
up
6
mpalmer at cybersource dot com
5 years ago
- - - - - - - - - - - - - -

XML Data:
<data>
<Report ID="1">
    <Date>REVIEW</Date>
    <AuthorID>1</AuthorID>
</Report>
<Report ID="2">
    <Date>REVIEW</Date>
    <AuthorID>2</AuthorID>
</Report>
</data>

- - - - - - - - - - - - - -

<?php
$xmlDoc
= new DOMDocument();
$xmlDoc->load( 'data.xml' );

$searchNode = $xmlDoc->getElementsByTagName( "Report" );

foreach(
$searchNode as $searchNode )
{
   
$valueID = $searchNode->getAttribute('ID');

   
$xmlDate = $searchNode->getElementsByTagName( "Date" );
   
$valueDate = $xmlDate->item(0)->nodeValue;

   
$xmlAuthorID = $searchNode->getElementsByTagName( "AuthorID" );
   
$valueAuthorID = $xmlAuthorID->item(0)->nodeValue;
   
    echo
"$valueID - $valueDate - $valueAuthorID\n";
}
?>

- - - - - - - - - - - - - -

Output:

1 - REVIEW - 1
2 - REVIEW - 2

- - - - - - - - - - - - - -
up
-1
metron at underhive-planet dot com
2 years ago
You can also use DOMXPath objects to interleave foreach() in this way is slightly shorter. If you need a special element or attribute value. The variable $xquery needs xpath expression. This example filters all group elements as described in xpath query ($xquery), then it filters again the results by last if condition and stores all attribute names in an object variable of type array ($this->menu).

Benefit: Using class array variable $menu in other class methods. Whenever you need those fields.

XML:
<xml>
  <category menu="products">
    <groups>
      <group menu="vegetables">...</group>
    </groups>
  </category>
</xml>

<?php
public class example {

public
$menu;

function
_construct(){}

public function
getGroupValueByCategory($xpath_object,$category) {
$xquery = '//category[normalize-space(@menu)="'.$category.'"]/groups/group';
             if(
$elements = $xpobject->evaluate($xquery)){
                
$this->menu = array();
             }
             foreach (
$elements as $element) {
                 if(
$element->hasAttributes()){
                    
$attributes = $element->attributes;
                     if(!
is_null($attributes)){
                         foreach (
$attributes as $index=>$attr){
                             if(
$attr->name == 'menu'){
                                
$this->menu[] = $attr->value;
                             }
                         }
                     }
                 }
             }
}

}
?>

I used this to implement simple menu structure.
up
-1
info at kevinfilteau dot com
3 years ago
I want to retrieve value of name in my current language, or the default, or anything set if none of the above are true.

There is a sample XML file:

<product>
   <name lang="en">Candy</name>
   <name lang="fr">Bonbon</name>
   <name>Bonbon (no lang)</name>
</product>

There is my function, hope this can help. And if anybody want's to improve it, your welcom. The input elements are DOMNodeList returned from a previous DomXPath Query.

<?php
   
private function getValueByLang($elements) {

       
$a = array();

        if (!
is_null($elements)) {

            foreach (
$elements as $element) {
   
                if( !
is_null($element->attributes)) {
       
                   
$is_lang = false;               
       
                    foreach (
$element->attributes as $attrName => $attrNode) {
                   
                        if(
'lang' == $attrName ) {
                           
$a[$attrNode->value] = $element->nodeValue;
                           
$is_lang = true;
                        }
                    }
                   
                    if( !
$is_lang ) {
                       
$a[] = $element->nodeValue;           
                    }
                }
               
               
           
            }

           
// Returns current language if set.
           
if( array_key_exists($this->lang, $a)) {
               
$result = $a[$this->lang];
           
// Returns default language if set.
           
} elseif( array_key_exists($this->default_lang, $a)) {
               
$result = $a[$this->default_lang];
           
// Returns first result with no language attribute.
           
} elseif( isset($a[0])) {
               
$result = $a[0];
           
// Return anything set.
           
} elseif( $result = reset($a) ) {
           
// Return nothing,  Doh!
           
} else {
               
$result = false;
           
            }
           
           
            return(
$result);
   
        } else {
            throw new
Exception(printf("No elements defined."));
        }
    }
?>

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