简单的XML返回与PHP和DOMDocument


Simple XML return with PHP and DOMDocument

我正在为我的应用程序构建一个简单的api Post/Return XML。下面是代码:

        $returnData = array (
        "ResultCode" => "0",
        "ResultString" => "uppdated"
        );
        $xml = new DOMDocument();
        $dateInfoElement = $xml->createElement("versionCheckResult");
        foreach ($returnData as $key => $value) {
            $xmlNode = $xml->createElement($key,$value);
            $dateInfoElement->appendChild($xmlNode);
            }
        $xml->appendChild($dateInfoElement); 
        echo $xml;
遗憾的是,我没有得到任何回报,什么都没有。Php不是我的强项,但它似乎比使用Node.JS和mongoDB更容易。你能告诉我我哪里做错了吗?

如果使用DOMDocument,则需要使用此方法将XML显示为字符串:DOMDocument::saveXML()

    $returnData = array (
    "ResultCode" => "0",
    "ResultString" => "uppdated"
    );
    $xml = new DOMDocument();
    $dateInfoElement = $xml->createElement("versionCheckResult");
    foreach ($returnData as $key => $value) {
        $xmlNode = $xml->createElement($key,$value);
        $dateInfoElement->appendChild($xmlNode);
        }
    $xml->appendChild($dateInfoElement); 
    echo $xml->saveXML(); //This should works as expected