PHP DOMDocument getElementsByTagname? [英] PHP DOMDocument getElementsByTagname?
本文介绍了PHP DOMDocument getElementsByTagname?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这正在推动我笨蛋...我只想添加另一个 img
节点。
This is driving me bonkers... I just want to add another img
node.
$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();
错误:
推荐答案
DOMDocument :: getElementsByTagName不返回数组,它返回一个的DOMNodeList 。您需要使用项目
方法来访问其项目:
DOMDocument::getElementsByTagName doesn't return an array, it returns a DOMNodeList. You need to use the item
method to access its items:
$album = $xmlDoc->getElementsByTagname('album')->item(0);
这篇关于PHP DOMDocument getElementsByTagname?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文