English 中文(简体)
能够改变具有PHP的xml值
原标题:Can t changing an xml value with PHP
  • 时间:2012-04-30 18:27:43
  •  标签:
  • php
  • xml
  • dom

This is my XML-file:

<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> 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个小时,没有找到解决我问题的办法。

最佳回答

$todo->erledigt don t work in DOMDocument, only briefXML can do that. 您需要再次使用<条码>。

您还需要使用<代码>nodeValue来获取/确定该要素的价值。

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

DEMO: http://codepad.org/xJbQmO2u

问题回答

在你寻找文件内的具体内容时,我建议通过使用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

选择所有<代码>erledigt中属于<代码>todo的节点 <代码>ttel和>stellt Node Value as specified.

那么,你可以轻易改变。 此外,你还重新寻找<代码>erledigt nodess that aren t “erledigt”,但现行变量已经没有受到伤害。

http://codepad.org/Dsby1NR4”rel=“nofollow”> • 《民主/未来法典》:

<?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();




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签