使用DOM创建XML文件时没有输出


There is no output while creating a XML file using DOM

我正在尝试测试是否创建了XML文档,但它不起作用,syntex中有问题吗?

function createFile()
{
    // create new dom document
    $xml = new DOMDocument();
    // these lines would create a nicely indented XML file
    $xml->preserveWhiteSpace = false;
    $xml->formatOutput = true;
    // create a root element
    $root = $xml->createElement("phonebook");
    // add root to DOM document root
    $xml->appendChild($root);
    // save dom document to an xml file
    $xml->save('out.xml');
}

我不知道你从哪里得到这么多代码
您的代码语法是正确的,一切都很好,为了获得创建文件所需的输出
将此添加到文件末尾:-

createFile();

您将看到一个输出文件,内容如下:-

<?xml version="1.0"?>
<phonebook/>