Php 在文件顶部添加xml节点或反转循环
我正在使用xml。读取xml文档可以很好地工作,添加节点可以很好地工作,但我希望将节点添加到xml文件的顶部。这是可能的还是不可能的 我之所以希望这样做,是因为当我显示xml文件时,我希望最后添加的节点(显示为最新节点)位于顶部 我使用以下循环显示xml:Php 在文件顶部添加xml节点或反转循环,php,xml,foreach,nodes,Php,Xml,Foreach,Nodes,我正在使用xml。读取xml文档可以很好地工作,添加节点可以很好地工作,但我希望将节点添加到xml文件的顶部。这是可能的还是不可能的 我之所以希望这样做,是因为当我显示xml文件时,我希望最后添加的节点(显示为最新节点)位于顶部 我使用以下循环显示xml: foreach($xml->xpath("//user[@id='12345678']") as $user){ foreach($user->children() as $action => $data){
foreach($xml->xpath("//user[@id='12345678']") as $user){
foreach($user->children() as $action => $data){
echo"<li>";
echo $data->content;
echo $data->date;
echo"</li>";
}
}
foreach($xml->xpath(//user[@id='12345678'])作为$user){
foreach($user->children()作为$action=>$data){
回声“”;
echo$数据->内容;
echo$data->date;
回声“ ”;
}
}
如果有一种方法可以反转循环,或者有另一种方法我对此很满意,那么不必在顶部添加第一个节点。下面是我如何添加节点的文件和xml文件的结构
有人知道如何解决这个问题吗
addxml.php
<?php
$file = "actielijst.xml";
$fp = fopen($file, "rb") or die("cannot open file");
$str = fread($fp, filesize($file));
$xml = new DOMDocument();
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->loadXML($str) or die("Error");
// get document element
echo "<xmp>OLD:\n". $xml->saveXML() ."</xmp>";
$root = $xml->documentElement;
$content = $xml->createElement("content");
$contentText = $xml->createTextNode("Nieuwe Factuur Mei");
$content->appendChild($contentText);
$date = $xml->createElement("date");
$dateText = $xml->createTextNode("23-12-2010");
$date->appendChild($dateText);
$action = $xml->createElement("action");
$action->appendChild($date);
$action->appendChild($content);
$root->appendChild($action);
$xml->save("actielijst.xml") or die("Error");
?>
formatOutput=true;
$xml->preserveWhiteSpace=false;
$xml->loadXML($str)或die(“错误”);
//获取文档元素
回显“旧:\n”$xml->saveXML();
$root=$xml->documentElement;
$content=$xml->createElement(“内容”);
$contentText=$xml->createTextNode(“Nieuwe Factuur Mei”);
$content->appendChild($contentText);
$date=$xml->createElement(“日期”);
$dateText=$xml->createTextNode(“23-12-2010”);
$date->appendChild($dateText);
$action=$xml->createElement(“action”);
$action->appendChild($date);
$action->appendChild($content);
$root->appendChild($action);
$xml->save(“actielijst.xml”)或die(“Error”);
?>
actielijst.xml
<?xml version="1.0"?>
<userid>
-------> Insert new action here <------
<action>
<date>23-01-2010</date>
<content>nieuwe factuur</content>
</action>
<action>
<date>23-01-2010</date>
<content>karten op 01-02</content>
</action>
</userid>
------->在此处插入新操作您可以使用xpath捕获每个父节点(本例中的操作),然后反转数组
$users_arr = array_reverse($xml->xpath("action"));
现在你可以在这个数组中循环 这会有帮助
<?php
$file = "actielijst.xml";
$fp = fopen($file, "rb") or die("cannot open file");
$str = fread($fp, filesize($file));
$xml = simplexml_load_string($str);
$action = $xml->addChild('action');
$action->addChild('content','sundar');
$action->addChild('date','23-12-2010');
header('content-type: application/xml');
echo $xml->saveXML();
addChild('action');
$action->addChild('content','sundar');
$action->addChild('date','23-12-2010');
标题('content-type:application/xml');
echo$xml->saveXML();
然后像这样:foreach($users\u arr as$user){?因为这样做不起作用..抱歉,基于您的XML,xpath值需要是action,而不是userid…已相应地编辑了上述代码它现在在数组中循环并显示4个li组件,就像XML中的for规则一样,但是echo$数据是空的。好的,echo$data->date;和$data->content;是空的。如果我只echo$data,它将是空的显示彼此相邻的所有内容,如:日期内容日期内容,如果我回显中断,它将中断所有四个元素。如何获得日期内容,如日期内容
日期内容
?SimpleXMLElement对象([0]=>23-01-2010)SimpleXMLElement对象([0]=>karten op 01-02)SimpleXMLElement对象([0]=>23-01-2010)SimpleXMLElement对象([0]=>nieuwe factuur)