📜  PHP | XMLReader XML()函数

📅  最后修改于: 2022-05-13 01:56:42.799000             🧑  作者: Mango

PHP | XMLReader XML()函数

XMLReader::XML()函数是PHP中的一个内置函数,用于设置包含要解析的 XML 的数据。 XML()函数的用途与 open函数相同,但唯一的区别是前者接受 XML 作为字符串,而后来接受它作为单独的.xml文件。

句法:

bool XMLReader::XML( string $source, 
string $encoding, int $options )

参数:此函数接受三个参数,如上所述,如下所述:

  • $source:它指定包含要解析的 XML 的字符串。
  • $encoding(可选):它指定文档编码或NULL。
  • $options(可选):它指定可选的位掩码。

返回值:此函数在成功时返回 TRUE,在失败时返回 FALSE。

例外:此函数在静态调用时会引发 E_STRICT 错误。

下面的例子说明了PHP中的XMLReader::XML()函数

示例 1:


    

GeeksforGeeks

";    // Open the XML file $XMLReader->XML($XML);    // Iterate through the XML nodes while ($XMLReader->read()) {     if ($XMLReader->nodeType == XMLREADER::ELEMENT) {            echo "We are at " . $XMLReader->name . "
";        } } ?>

输出:

We are at div
We are at p

示例 2:


    

GeeksforGeeks

";    // Open the XML file $XMLReader->XML($XML);    // Read the nodes $XMLReader->read();    // Read it as a string $string = $XMLReader->readString();     // Output the string to the browser echo $string; ?>

输出:

GeeksforGeeks

参考: https://www. PHP.net/manual/en/xmlreader.xml。 PHP