在我问这个问题之前我已经在这里搜索了......我知道这里有一个答案..但是我找不到它..
我希望能够在我的xml文件中通过id编辑子节点..我尝试了很多脚本但是我的时间非常糟糕..
请在edit.php中看到这一行:
echo "the problem is in the next line";
这是服务器读取的最后一行..
感谢您的帮助。
events.xml
<?xml version="1.0" encoding="UTF-8" ?>
<events>
<record>
<id>1</id>
<event>a</event>
<eventDate>a</eventDate>
<desc>a</desc>
</record>
<record>
<id>2</id>
<event>b</event>
<eventDate>b</eventDate>
<desc>b</desc>
</record>
</events>
edit.php
header("Content-type: text/html; charset=utf-8");
$record = array(
'id' => $_POST['id'],
'event' => $_POST['event'],
'eventDate' => $_POST['eventDate'],
'desc' => $_POST['desc'],
);
$id = $record["id"];
$dom = new DOMDocument;
$dom->load('events.xml');
$xpath = new DOMXPath($dom);
$query = sprintf('/events/record[./id = "%d"]', $id);
foreach($xpath->query($query) as $record) {
$eventN = $record->parentNode->getElementsByTagName("event");
echo "the problem is in the next line";
$eventN->item(0)->nodeValue = $record["event"];
$dateN = $record->parentNode->getElementsByTagName("eventDate");
$dateN->item(0)->nodeValue = $record["eventDate"];
$descN = $record->parentNode->getElementsByTagName("desc");
$descN->item(0)->nodeValue = $record["desc"];
}
$dom->save("events.xml");
header("Location: {$_SERVER['HTTP_REFERER']}");
?>
编辑:使用edit.php但不是动态
<?php
header("Content-type: text/html; charset=utf-8");
$record = array(
'id' => $_POST['id'],
'event' => $_POST['event'],
'eventDate' => $_POST['eventDate'],
'desc' => $_POST['desc'],
);
$id = $record["id"];
$dom = new DOMDocument;
$dom->load('events.xml');
$xpath = new DOMXPath($dom);
$query = sprintf('/events/record[./id = "%d"]', $id);
foreach($xpath->query($query) as $record) {
$eventN = $record->parentNode->getElementsByTagName("event");
echo "i change it to string text and it's works. ";
$eventN->item(0)->nodeValue = 'text';
$dateN = $record->parentNode->getElementsByTagName("eventDate");
$dateN->item(0)->nodeValue = 'text';
$descN = $record->parentNode->getElementsByTagName("desc");
$descN->item(0)->nodeValue = 'text';
}
$dom->save("events.xml");
header("Location: {$_SERVER['HTTP_REFERER']}");
?>
答案 0 :(得分:6)
好的,问题是您的$record
数组从一开始就会被foreach
覆盖。
更改为$record
或更改包含$_POST
数据的数组的名称。
在旁注中,您正在迭代元素,因此没有理由进入parentNode。使用
$record->getElementsByTagName("event");