php中isvalid(),PHP XMLReader isValid()用法及代码示例

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


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部