【发布时间】:2015-05-29 11:48:31
【问题描述】:
我无法将 DOMDocument parentNode 从 null 更改。我尝试过同时使用appendChild 和replaceChild,但没有任何运气。
我哪里错了?
error_reporting(E_ALL);
function xml_encode($mixed, $DOMDocument=null) {
if (is_null($DOMDocument)) {
$DOMDocument =new DOMDocument;
$DOMDocument->formatOutput = true;
xml_encode($mixed, $DOMDocument);
echo $DOMDocument->saveXML();
} else {
if (is_array($mixed)) {
$node = $DOMDocument->createElement('urlset', 'hello');
$DOMDocument->parentNode->appendChild($node);
}
}
}
$data = array();
for ($x = 0; $x <= 10; $x++) {
$data['urlset'][] = array(
'loc' => 'http://www.example.com/user',
'lastmod' => 'YYYY-MM-DD',
'changefreq' => 'monthly',
'priority' => 0.5
);
}
header('Content-Type: application/xml');
echo xml_encode($data);
?>
【问题讨论】:
-
你不会从 esle 块返回任何东西
-
我找不到变量
parentNode... -
在 else 块之后移动
echo $DOMDocument->saveXML(); -
有点混乱...你能显示预期的输出吗?
-
@Michael Wilson 我试试你的链接,我是对的 - 移动后它可以工作