這讓我瘋狂......我只想添加另一個img
節點。PHP DOMDocument getElementsByTagname?
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<gallery>
<album tnPath="tn/" lgPath="imm/" fsPath="iml/" >
<img src="004.jpg" caption="4th caption" />
<img src="005.jpg" caption="5th caption" />
<img src="006.jpg" caption="6th caption" />
</album>
</gallery>
XML;
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
$album = $xmlDoc->getElementsByTagname('album')[0];
// Parse error: syntax error, unexpected '[' in /Applications/XAMPP/xamppfiles/htdocs/admin/tests/DOMDoc.php on line 17
$album = $xmlDoc->getElementsByTagname('album');
// Fatal error: Call to undefined method DOMNodeList::appendChild() in /Applications/XAMPP/xamppfiles/htdocs/admin/tests/DOMDoc.php on line 19
$newImg = $xmlDoc->createElement("img");
$album->appendChild($newImg);
print $xmlDoc->saveXML();
錯誤:
'$ xmlDoc-> getElementsByTagname('album')[0];'現在在PHP7中工作:) – 2016-04-01 15:36:18