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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值