📜  PHP | DOMDocument loadXML()函数

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

PHP | DOMDocument loadXML()函数

DOMDocument::loadXML()函数是PHP中的一个内置函数,用于从字符串加载 XML 文件。

句法:

mixed DOMDocument::loadXML( string $source, int $options = 0 )

参数:该函数接受上面提到的两个参数,如下所述:

  • $source:此参数保存包含 XML 文档的字符串。
  • $options:此参数保存 libxml 选项常量的按位或。

返回值:此函数在成功时返回 TRUE,在失败时返回 FALSE。如果静态调用此函数或失败时返回 FALSE,则此函数返回 DOMDocument。

下面的程序说明了PHP中的 DOMDocument::loadXML()函数:

方案一:

loadXML(
" 
    Geeks123 
    GeeksforGeeks  
    
           +91-XXXXXXXXXX         abc@geeksforgeeks.org     
 
");     // Create XML file and display it echo $doc->saveHTML();     ?>
输出:
 
    Geeks123 
    GeeksforGeeks  
    
+91-XXXXXXXXXX abc@geeksforgeeks.org

方案二:

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

参考: https://www. PHP.net/manual/en/domdocument.loadxml。 PHP