gpt4 book ai didi

php - 无法使用 PHP 更改 xml 值

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:06:24 27 4
gpt4 key购买 nike

这是我的 XML 文件:

<todos>
<todo>
<titel>sasd</titel>
<erstellt>2012-04-30 17:19:21</erstellt>
<erledigen_bis>2012-05-03</erledigen_bis>
<erledigt>Nein</erledigt>
<thingstodo>sffsdfdf</thingstodo>
</todo>
</todos>

现在我想更改 <erledigt> 中的值标记为“Ja”。

我尝试使用以下代码:

<?php
$filename = 'xml/todos.xml';

$xmlDoc = new DOMDocument();
$xmlDoc->load('xml/todos.xml');

$todos = $xmlDoc->getElementsByTagName('todo');

foreach ($todos as $todo) {

$titel = $todo->getElementsByTagName('titel');
$actualTitel = $titel->item(0)->nodeValue;
$paramTitel = $_GET["titel"];

$erstellt = $todo->getElementsByTagName('erstellt');
$actualTimestamp = $erstellt->item(0)->nodeValue;
$paramTimestamp = $_GET["timestamp"];

if ($paramTitel == $actualTitel && $paramTimestamp == $actualTimestamp) {

$todo->erledigt= 'Ja';

}
}

$xmlDoc->save($filename);

header('Location: todo.php');
?>

请帮助我,我在网上搜索了大约 5 个小时,但找不到解决我问题的方法。

最佳答案

$todo->erledigt 在 DOMDocument 中不起作用,只有 SimpleXML 可以做到这一点。您需要再次使用 getElementsByTagName

您还需要使用 nodeValue 来获取/设置元素的值。

if ($paramTitel == $actualTitel && $paramTimestamp == $actualTimestamp) {
$todo->getElementsByTagName('erledigt')->item(0)->nodeValue = 'Ja';
}

演示:http://codepad.org/xJbQmO2u

关于php - 无法使用 PHP 更改 xml 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10388661/

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