XMLReader::getAttribute()函数是PHP中的一个内置函数,用于获取命名属性的值。
用法:
string XMLReader::getAttribute( string $name )
参数:该函数接受单个参数$name,该参数保存属性的名称。
返回值:此函数返回属性的值;如果在元素节点上找不到或没有给定名称的属性,则返回NULL。
以下示例说明了PHP中的XMLReader::getAttribute()函数:
范例1:
data.xml
Hello
index.php
// Create a new XMLReader instance
$XMLReader = new XMLReader();
// Load the XML file
$XMLReader->open('data.xml');
// Iterate through the XML
while ($XMLReader->read()) {
if ($XMLReader->nodeType == XMLREADER::ELEMENT) {
// Get the value of attribute with name id
$value = $XMLReader->getAttribute('id');
// Output the value to browser
echo $value;
}
}
?>
输出:
// Empty string because there is no attributes with name id
程序2:
data.xml
Hello
World
index.php
// Create a new XMLReader instance
$XMLReader = new XMLReader();
// Load the XML file
$XMLReader->open('data.xml');
// Iterate through the XML
while ($XMLReader->read()) {
if ($XMLReader->nodeType == XMLREADER::ELEMENT) {
// Get the value of attribute with name id
$value = $XMLReader->getAttribute('id');
// Output the value to browser
echo $value . "
";
}
}
?>
输出:
geeksforgeeks
my_id