略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: DOMElement::getAttribute

2024-04-28

DOMElement::getAttribute

(PHP 5, PHP 7, PHP 8)

DOMElement::getAttributeReturns value of attribute

说明

public DOMElement::getAttribute(string $qualifiedName): string

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

参数

qualifiedName

The name of the attribute.

返回值

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

参见

add a noteadd a note

User Contributed Notes 1 note

up
6
mpalmer at cybersource dot com
14 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

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

官方地址:https://www.php.net/manual/en/domelement.getattribute.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3