我在以下代码段中收到错误:
1 $xml = new DOMDocument('1.0', 'utf-8');
2 $xml->formatOutput = TRUE; // enable automatic indenting
3 $xml->loadXML('<songData/>'); // set root node
4 $song = $xml->addChild('song');
5 $artist = $xml->addChild('artist');
6
7 echo $xml->asXML();
返回:
Call to undefined method DOMDocument::addChild() on line 4
触发此错误的原因是什么?
编辑,最终我要做的事情是这样的:
<?xml version="1.0" encoding="utf-8"?>
<songData>
<song>
<title>Carbona Not Glue</title>
<artist>Ramones</artist>
<members>
<name>Johny</name>
<name>Joey</name>
<name>Dee Dee</name>
<name>Marky</name>
</members>
</song>
</songData>
答案 0 :(得分:0)
您准备创建哪种XML,您可以发布一个示例吗?如果您想要<songData><song><artist></artist></song></songData>
之类的内容,则可以使用createElement
函数执行此操作:
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = TRUE; // enable automatic indenting
// Create the element
$rootElement = $xml->createElement('songData'); // set root node
$songElement = $xml->createElement('song');
$artistElement = $xml->createElement('artist');
// Add them to the DOM
$xml->appendChild($rootElement);
$rootElement->appendChild($songElement);
$songElement->appendChild($artistElement);
echo $xml->saveXML();