XMLReader::isValid()函数是PHP中的一个内置函数,用于检查所解析的文档是否有效。如果遵循定义所有允许的元素和元素结构的DTD(文档类型定义)来编写XML,则该XML是有效的。
用法:
bool XMLReader::isValid( void )
参数:此函数不接受任何参数。
返回值:如果成功,则此函数返回TRUE;如果失败,则返回FALSE。
下面给出的程序说明了PHP中的XMLReader::isValid()函数:
程序1:
文档名称: data.xml
]>
Hi
World
GeeksforGeeks
文档名称: index.php
// Create a new XMLReader instance
$XMLReader = new XMLReader();
// Open the XML file
$XMLReader->open('data.xml');
// Enable the Parser Property
$XMLReader->setParserProperty(
XMLReader::VALIDATE, true);
// Iterate through the XML nodes
while ($XMLReader->read()) {
if ($XMLReader->nodeType == XMLREADER::ELEMENT) {
// Check if XML is valid or not
$isValid = $XMLReader->isValid();
if ($isValid) {
echo "YES ! this node is validated
";
}
}
}
?>
输出:
YES ! this node is validated
YES ! this node is validated
YES ! this node is validated
YES ! this node is validated
YES ! this node is validated
程序2:
文档名称: data.xml
]>
文档名称: index.php
// Create a new XMLReader instance
$XMLReader = new XMLReader();
// Open the XML file
$XMLReader->open('data.xml');
// Enable the Parser Property
$XMLReader->setParserProperty(
XMLReader::VALIDATE, true);
// Iterate through the XML nodes
while ($XMLReader->read()) {
if ($XMLReader->nodeType == XMLREADER::ELEMENT) {
// Check if XML is valid or not
$isValid = $XMLReader->isValid();
if (!$isValid) {
echo "No ! this node is not validated
";
}
}
}
?>
输出:
No ! this node is not validated
No ! this node is not validated