DOMDocument::loadXML()函数是PHP中的内置函数,用于从字符串中加载XML文件。
用法:
mixed DOMDocument::loadXML( string $source, int $options = 0 )
参数:该函数接受上述和以下描述的两个参数:
$source:此参数保存包含XML文档的字符串。
$options:此参数保存libxml选项常量的按位或。
返回值:如果成功,则此函数返回TRUE;如果失败,则返回FALSE。如果以静态方式调用该函数,则返回DOMDocument;如果失败,则返回FALSE。
以下示例程序旨在说明PHP中的DOMDocument::loadXML()函数:
示例1:
// Create a new DOMDocument
$doc = new DOMDocument();
// Load the XML file
$doc->loadXML(
"
Geeks123
GeeksforGeeks
+91-XXXXXXXXXX
abc@geeksforgeeks.org
");
// Create XML file and display it
echo $doc->saveHTML();
?>
输出:
Geeks123
GeeksforGeeks
+91-XXXXXXXXXX
abc@geeksforgeeks.org
示例2:
// Create new DOMDocument
$doc = new DOMDocument();
// Create a comment document
$comm1 = $doc->createComment('Starting of XML document');
// Append element to the document
$doc->appendChild($comm1);
// Create XML file and display it
echo $doc->saveHTML();
// Load the XML file
$doc->loadXML(
"
Geeks123
GeeksforGeeks
+91-XXXXXXXXXX
abc@geeksforgeeks.org
");
// Create comment element
$comm2 = $doc->createComment('Ending of XML document');
// Append element to the document
$doc->appendChild($comm2);
// Create XML element and display it
echo $doc->saveHTML();
?>
输出:
Geeks123
GeeksforGeeks
+91-XXXXXXXXXX
abc@geeksforgeeks.org