Basic example of use :
<?xml version="1.0" encoding="UTF-8"?>
<racine version="2.0a">
<article/>
</racine>
<?php
(...)
echo $doc->documentElement->attributes->getNamedItem("version")->nodeValue;
// returns "2.0a"
?>PHP - Manual: DOMNamedNodeMap::getNamedItem
2025-10-27
(PHP 5, PHP 7, PHP 8)
DOMNamedNodeMap::getNamedItem — Retrieves a node specified by name
Retrieves a node specified by its nodeName.
qualifiedName
The nodeName of the node to retrieve.
A node (of any type) with the specified nodeName, or
null if no node is found.
示例 #1 Getting an attribute on a node
<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$id = $doc->firstChild->attributes->getNamedItem('id');
?>示例 #2 Accessing attribute with array syntax
<?php
$id = $doc->firstChild->attributes['id'];
?>Basic example of use :
<?xml version="1.0" encoding="UTF-8"?>
<racine version="2.0a">
<article/>
</racine>
<?php
(...)
echo $doc->documentElement->attributes->getNamedItem("version")->nodeValue;
// returns "2.0a"
?>官方地址:https://www.php.net/manual/en/domnamednodemap.getnameditem.php