PHP | XMLReader next()函数
XMLReader::next()函数是PHP中的一个内置函数,用于将光标移动到跳过所有子树的下一个节点。这个函数的另一个用途是它接受节点的名称直接移动到元素。
句法:
bool XMLReader::next( string $localname )
参数:此函数接受单个参数$localname ,它保存下一个要移动的节点的名称。
返回值:此函数在成功时返回 TRUE,在失败时返回 FALSE。
下面的程序说明了PHP中的XMLReader::next()函数:
程序 1:在这个程序中,我们将遍历 XML 树。
文件名: data.xml
html
Foo Bar
Foo Bar
php
open('data.xml');
// Iterate through the XML nodes
// to reach the h1 node
$XMLReader->read();
$XMLReader->read();
$XMLReader->next();
// Print name of element
echo "Before:
We are currently"
. " at: $XMLReader->name
";
// Move to next node which is
// text "Foo Bar"
$XMLReader->next();
// Move to next element
// which is div2
$XMLReader->next();
// Print name of element
echo "After:
We are currently"
. " at: $XMLReader->name";
?>
html
Foo Bar
Foo Bar
Hello
php
open('data.xml');
// Iterate through the XML nodes to
// reach the subtrees of div1
$XMLReader->read();
$XMLReader->read();
$XMLReader->next("div2");
// Print name of element
echo "Before:
We are currently"
. "at: $XMLReader->name
";
// Move to next node which is
// text "Foo Bar"
$XMLReader->next();
// Move to next element which is div2
$XMLReader->next();
// Print name of element
echo "After:
We are currently"
. " at: $XMLReader->name";
?>
文件名:索引。 PHP
PHP
open('data.xml');
// Iterate through the XML nodes
// to reach the h1 node
$XMLReader->read();
$XMLReader->read();
$XMLReader->next();
// Print name of element
echo "Before:
We are currently"
. " at: $XMLReader->name
";
// Move to next node which is
// text "Foo Bar"
$XMLReader->next();
// Move to next element
// which is div2
$XMLReader->next();
// Print name of element
echo "After:
We are currently"
. " at: $XMLReader->name";
?>
输出:
前:
We are currently at: h1
后:
We are currently at: div2
程序 2:在这个程序中,我们将移动到 XML 树中的特定节点。
文件名: data.xml
html
Foo Bar
Foo Bar
Hello
文件名:索引。 PHP
PHP
open('data.xml');
// Iterate through the XML nodes to
// reach the subtrees of div1
$XMLReader->read();
$XMLReader->read();
$XMLReader->next("div2");
// Print name of element
echo "Before:
We are currently"
. "at: $XMLReader->name
";
// Move to next node which is
// text "Foo Bar"
$XMLReader->next();
// Move to next element which is div2
$XMLReader->next();
// Print name of element
echo "After:
We are currently"
. " at: $XMLReader->name";
?>
输出:
前:
We are currently at: div2
后:
We are currently at: div3
参考: https://www. PHP.net/manual/en/xmlreader.next。 PHP