XMLReader::XML()函数是PHP中的内置函数,用于设置包含要解析的XML的数据。 XML()函数的功能与打开函数的目的相同,但唯一的区别是前者将XML接受为字符串,而后者将其接受为单独的.xml文件。
用法:
bool XMLReader::XML( string $source,
string $encoding, int $options )
参数:此函数接受上述和以下所述的三个参数:
$source:它指定包含要解析的XML的字符串。
$encoding (Optional):它指定文档编码或NULL。
$options (Optional):它指定可选的位掩码。
返回值:如果成功,此函数将返回TRUE,否则将返回FALSE。
异常:静态调用此函数会引发E_STRICT错误。
以下示例说明了PHP中的XMLReader::XML()函数:
范例1:
// Create a new XMLReader instance
$XMLReader = new XMLReader();
$XML = "<?xml version=\"1.0\"?>
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:
// Create a new XMLReader instance
$XMLReader = new XMLReader();
$XML = "<?xml version=\"1.0\"?>
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