PHP Doku:: Returns value of attribute - domelement.getattribute.html

Verlauf / Chronik / History: (2) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationDocument Object ModelThe DOMElement classDOMElement::getAttribute

Ein Service von Reinhard Neidl - Webprogrammierung.

The DOMElement class

<<DOMElement::__construct

DOMElement::getAttributeNode>>

DOMElement::getAttribute

(PHP 5)

DOMElement::getAttributeReturns value of attribute

Beschreibung

string DOMElement::getAttribute ( string $name )

Gets the value of the attribute with name name for the current node.

Parameter-Liste

name

The name of the attribute.

Rückgabewerte

The value of the attribute, or an empty string if no attribute with the given name is found.

Siehe auch


2 BenutzerBeiträge:
- Beiträge aktualisieren...
info at kevinfilteau dot com
18.07.2009 7:25
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."));
        }
    }
?>
mpalmer at cybersource dot com
9.11.2007 12:32
- - - - - - - - - - - - - -

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

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



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",...)