Can';t使用PHP更改xml值

Can';t使用PHP更改xml值,php,xml,dom,Php,Xml,Dom,这是我的XML文件: <todos> <todo> <titel>sasd</titel> <erstellt>2012-04-30 17:19:21</erstellt> <erledigen_bis>2012-05-03</erledigen_bis> <erledigt>Nein</erledigt> <thingstod

这是我的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>

sasd
2012-04-30 17:19:21
2012-05-03
不
sffsdfdf
现在我想将
标记内的值更改为“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');
?>
load('xml/todos.xml');
$todos=$xmlDoc->getElementsByTagName('todo');
foreach($todo作为$todo){
$titel=$todo->getElementsByTagName('titel');
$actualTitel=$titel->item(0)->nodeValue;
$paramitel=$_GET[“titel”];
$erstellt=$todo->getElementsByTagName('erstellt');
$actualTimestamp=$erstellt->item(0)->nodeValue;
$paramtimstamp=$_GET[“timestamp”];
如果($paramitel==$actualTitel&&$paramtimstamp==$actualTimestamp){
$todo->erledigt='Ja';
}
}
$xmlDoc->save($filename);
标题('Location:todo.php');
?>

请帮帮我,我在网上搜索了大约5个小时,没有找到任何解决问题的方法。

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

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

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

演示:

您需要使用DOM方法和属性来访问
元素。看起来你把它和SimpleXML混淆了

$todo->getElementsByTagName('erledigt')->item(0)->nodeValue = 'Ja';

如果您确实想看看SimpleXML的使用,那么您的整个代码可能看起来像

<?php

$paramTitel = $_GET["titel"];
$paramTimestamp = $_GET["timestamp"];

$todos = simplexml_load_file('xml/todos.xml');
foreach ($todos->todo as $todo) {
    if ($paramTitel == (string) $todo->titel 
     && $paramTimestamp == (string) $todo->erstellt
    ) {
        $todo->erledigt = 'Ja';
    }
}

$todos->asXML($filename); // Where does $filename come from?

header('Location: todo.php');

?>
todo作为$todo){
如果($paramitel==(字符串)$todo->titel
&&$paramTimestamp==(字符串)$todo->erstellt
) {
$todo->erledigt='Ja';
}
}
$todos->asXML($filename);//$filename来自哪里?
标题('Location:todo.php');
?>

阅读:


当您在文档中查找特定元素时,我强烈建议您使用xpath定位它们:

$xp = new DOMXPath($doc);
$expression = sprintf('//todo[titel = "%s" and erstellt = "%s"]/erledigt', $_GET["titel"], $_GET["timestamp"]);
$result = $xp->query($expression);
foreach ($result as $node) {
    $node->nodeValue = 'Ja';
}
这将导致以下(示例性)xpath表达式:

//todo[titel = "sasd" and erstellt = "2012-04-30 17:19:21"]/erledigt
它将选择属于
todo
节点的所有
erledigt
节点,这些节点具有指定的
titel
erstellt
节点值

然后,您可以轻松地更改它。此外,您正在查找尚未“erledigt”但当前变体尚未损坏的
erledigt
节点

:

loadXML($xml);
$xp=新的DOMXPath($doc);
$expression=sprintf('//todo[titel=“%s”和erstellt=“%s”]/erledigt',$获取[“titel”],$获取[“timestamp”]);
$result=$xp->query($expression);
foreach($result作为$node){
$node->nodeValue='Ja';
}
echo$doc->saveXML();

我认为,对于那些不知道使用哪个XML扩展的人来说,通过XPath进行搜索不是一个好主意。TS使用DOMDocument,所以我选择了“that”XPath,简单XML也有XPath,它同样简单(同样的表达式也简化了simplexml代码)整个下午我都在研究XPath,但这对我来说太复杂了,因为这是我第一个使用XML、PHP和JS的项目,所以我真正开始了我的web开发之旅。但是很高兴看到使用XPath的解决方案,感谢您提供了有趣但又复杂的内容;)@阿尔德罗斯:不客气。无论你是刚开始还是已经做了很多年,总有一些东西需要学习非常感谢你,你把我从一个不眠之夜中救了出来!在复制和粘贴之前,我想更正值,而不是$filename,但我只是删除了它,以后没有编辑它,如果我把你弄糊涂了,很抱歉。SimpleXML解决方案非常好,做得很好,谢谢。
<?php
/**
 * @link http://stackoverflow.com/q/10388661/367456
 */

$_GET["titel"] = 'sasd';
$_GET["timestamp"] = '2012-04-30 17:19:21';

$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>';

$doc = new DOMDocument();
$doc->loadXML($xml);
$xp = new DOMXPath($doc);
$expression = sprintf('//todo[titel = "%s" and erstellt = "%s"]/erledigt', $_GET["titel"], $_GET["timestamp"]);
$result = $xp->query($expression);
foreach ($result as $node) {
    $node->nodeValue = 'Ja';
}

echo $doc->saveXML();