4

これは私の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 時間検索しましたが、問題の解決策が見つかりませんでした。

4

3 に答える 3

3

$todo->erledigtDOMDocumentでは機能しません。SimpleXMLでのみ機能します。もう一度使用する必要がありますgetElementsByTagName

nodeValueまた、要素の値を取得/設定するためにを使用する必要があります。

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

デモ:http ://codepad.org/xJbQmO2u

于 2012-04-30T18:39:36.217 に答える
3

ドキュメント内の特定の要素を探しているので、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の一部であるすべてのノードが選択されます。todotitelerstellt

その後、簡単に変更できます。erledigtさらに、まだ「erledigt」ではないノードを探していますが、現在のバリアントはすでに問題ありません。

デモ/完全なコード例:

<?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();
于 2012-04-30T18:47:18.100 に答える
3

<erledigt>要素にアクセスするには、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');

?>

読む:

于 2012-04-30T18:44:16.183 に答える