[英]How to Edit xml child node with php
i already search here before i ask this question.. I know there is an answer here somewhere.. but i can't find it.. 在问这个问题之前,我已经在这里搜索了。我知道这里有一个答案..但是我找不到它。
I want to be able to edit child node by id in my xml file.. i tried alot of script but i'm having a really bad time with this.. 我希望能够通过我的xml文件中的ID编辑子节点。.我尝试了很多脚本,但是与此同时,我的时间很不好。
please see this line in edit.php: 请在edit.php中看到这一行:
echo "the problem is in the next line";
this is the last line that the server reads.. 这是服务器读取的最后一行。
thanks you for your help. 谢谢您的帮助。
events.xml 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 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']}");
?>
Edited: Working edit.php but not dynamic 编辑:工作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']}");
?>
Ok, the issue is your $record
array from the beginning is overwritten in the foreach
. 好的,问题是您的$record
数组从一开始就被foreach
覆盖了。
Either change the as $record
or change the name of the array holding the $_POST
data. 可以将as更改为$record
也可以更改保存$_POST
数据的数组的名称。
On a sidenote, you are iterating over elements, so there is no reason to get to the parentNode. 在旁注中,您正在遍历元素,因此没有理由进入parentNode。 Use 采用
$record->getElementsByTagName("event");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.