2012-04-30 21 views
4

改變一個XML值這是我的XML文件:不能與PHP

<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小時,找不到我的問題的任何解決方案。

回答

3

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

您還需要使用nodeValue來獲取/設置元素的值。

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

DEMO:http://codepad.org/xJbQmO2u

+0

謝謝你這麼多,你從一個不眠之夜救了我! – alderos

+0

不客氣:-) –

3

您需要使用DOM方法和屬性來訪問它來編輯<erledigt>元素。看起來你正在將它與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'); 

?> 

閱讀:

+1

用於顯示SimpleXML示例的+1 :-) –

+0

在複製和粘貼之前,我希望改變值而不是$文件名,但是我只刪除了它並且沒有編輯它後來,如果我讓你困惑,很抱歉。 SimpleXML解決方案非常好,做得很好,謝謝你。 – alderos

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節點,它們是具有指定的titelerstellt節點值的todo節點的一部分。

然後,您可以輕鬆地更改它。此外,您正在尋找尚未「erledigt」的erledigt節點,但目前的版本已經不會受到影響。

Demo/Full Code Example

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

我不認爲通過XPath搜索某個不知道使用哪種XML擴展的人是不錯的主意。 – salathe

+0

TS使用DOMDocument,所以我選擇了「那個」xpath,它也具有xpath同樣簡單的simple-xml(同樣的表達也簡化了simplexml代碼)。 – hakre

+0

我在整個下午閱讀了關於XPath的一些關於我的研究的文章,但這對我來說很複雜,因爲這是我第一個使用XML,PHP和JS的項目,所以我真的開始通過web開發旅行。但很高興看到XPath的解決方案,謝謝你的有趣,但也是複雜的東西;) – alderos