gpt4 book ai didi

PHP DOMDocument getElementsByTagname?

转载 作者:可可西里 更新时间:2023-10-31 22:41:53 24 4
gpt4 key购买 nike

这让我抓狂...我只想添加另一个 img 节点。

$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 .您需要使用 item 方法来访问它的项目:

$album = $xmlDoc->getElementsByTagname('album')->item(0);

关于PHP DOMDocument getElementsByTagname?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2845908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com