无法使用PHP更改xml值 [英] Can't changing an xml value with PHP

查看:96
本文介绍了无法使用PHP更改xml值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的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> / code>标签为'Ja'。

Now I want to change the value inside of the <erledigt> tag to '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个小时,找不到任何解决方案我的问题。

Please help me, I searched for about 5 hours on the web and couldn't find any solution for my problem.

推荐答案

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

$todo->erledigt doesn't work in DOMDocument, only SimpleXML can do that. You need to use getElementsByTagName again.

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

You also need to use nodeValue to get/set the value of the element.

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

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

这篇关于无法使用PHP更改xml值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆