DOMDocument::saveXML()函数是PHP中的一个内置函数,用于从DOM表示形式创建XML文档。从头开始构建新的dom文档后使用此功能。
用法:
string DOMDocument::saveXML( DOMNode $node, int $options = 0 )
参数:该函数接受上述和以下描述的两个参数:
$node:此参数仅用于没有XML声明的特定节点的输出,而不是整个文档的输出。
$options:此参数添加其他选项。当前,此参数仅支持LIBXML_NOEMPTYTAG。
返回值:如果成功,此函数返回XML文档;如果失败,则返回FALSE。
以下示例程序旨在说明PHP中的DOMDocument::saveXML()函数:
示例1:
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
// Use createTextNode() function to create a text node
$domTN = $domDocument->createTextNode('GeeksforGeeks');
// Append element to the document
$domDocument->appendChild($domTN);
// Use saveXML() function to save the XML document
echo $domDocument->saveXML();
?>
输出:
GeeksforGeeks
示例2:
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
// Use createElement() function to create an element node
$domElement = $domDocument->createElement('organization',
'GeeksforGeeks');
// Append element to the document
$domDocument->appendChild($domElement);
// Use saveXML() function to create a XML document
echo $domDocument->saveXML();
?>
输出:
GeeksforGeeks