PHP | SimpleXMLElement::getName()函数
先决条件:阅读 XML 基础知识
SimpleXMLElement::getName()函数是PHP中的一个内置函数,它返回 xml 元素的名称。
句法:
string SimpleXMLElement::getName( void )
参数:此函数不接受任何参数。
返回值:它返回一个字符串,表示 SimpleXMLElement 对象的 XML 元素的名称。
注意:此函数在PHP 5.1.3 及更新版本上可用。
下面的程序说明了PHP中的 SimpleXMLElement::getName()函数:
示例 1:
php
Geeks123
GeeksforGeeks
+91-XXXXXXXXXX
Noide India
XML;
// Loading string as simple xml object
$xml = simplexml_load_string($user);
// Display the name of element
echo "Base tag name: " . $xml->getName() . "
";
foreach($xml->children() as $child) {
echo "child node: " . $child->getName()
. " = " . $child . "";
}
?>
php
Geeks123
GeeksforGeeks
+91-XXXXXXXXXX
Computer science portal
Noida
India
XML;
// Loading string as simple xml object
$xml = simplexml_load_string($user);
// Recursive function called
getname_rec($xml, 0);
// The getname_rec() function definition
function getname_rec($xml, $depth) {
print_space($depth);
echo "tag name: " . $xml->getName() . "
";
foreach($xml->children() as $child) {
if($child->count() > 0) {
// If there exists any child of current node
getname_rec($child, $depth+1);
}
else {
// If there is no child of the current node
print_space($depth);
echo " child node: " . $child->getName()
. " = " . $child . "";
}
}
}
// Function to print 3X$i number of spaces
function print_space($i) {
for($x = 0; $x < $i*3; $x++) {
echo " ";
}
}
?>
输出:
示例 2:
PHP
Geeks123
GeeksforGeeks
+91-XXXXXXXXXX
Computer science portal
Noida
India
XML;
// Loading string as simple xml object
$xml = simplexml_load_string($user);
// Recursive function called
getname_rec($xml, 0);
// The getname_rec() function definition
function getname_rec($xml, $depth) {
print_space($depth);
echo "tag name: " . $xml->getName() . "
";
foreach($xml->children() as $child) {
if($child->count() > 0) {
// If there exists any child of current node
getname_rec($child, $depth+1);
}
else {
// If there is no child of the current node
print_space($depth);
echo " child node: " . $child->getName()
. " = " . $child . "";
}
}
}
// Function to print 3X$i number of spaces
function print_space($i) {
for($x = 0; $x < $i*3; $x++) {
echo " ";
}
}
?>
输出:
参考: https://www. PHP.net/manual/en/simplexmlelement.getname。 PHP