PHP从XML删除元素

问题描述 投票:1回答:4

我想删除比今天更旧的任务,但是我还不太清楚如何从XML文件中删除元素。

$xml_file = simplexml_load_file('file.xml');    
foreach ($xml_file->task as $aTask) {
        if ($aTask->date < $today) {
            //$xml_file->removeChild($aTask); //doesnt work
        }
}

下面是我的XML文件:

<?xml version="1.0"?>
<calender>
<task><date>00/00/0000</date><title>My Birthday</title><description>Today is my birthday!</description></task><task><date>04/01/2013</date><title>ghh</title><description>jfhjhj</description></task><task><date>04/01/2013</date><title>egfwe</title><description>wefwef</description></task><task><date>04/01/2013</date><title>cvbhm</title><description>dhmdh</description></task><task><date>04/02/2013</date><title>gg</title><description>gg</description></task><task><date>04/02/2013</date><title>test</title><description>test</description></task><task><date>04/03/2013</date><title>ggg</title><description>ggg</description></task><task><date>04/03/2013</date><title>ssdv</title><description>ssdvs</description></task><task><date>04/03/2013</date><title>test</title><description>testtest</description></task><task><date>04/04/2013</date><title>tttt</title><description>ttttttttt</description></task><task><date>04/05/2013</date><title>qewerbqwer</title><description>bwerbwerbwebr</description></task><task><date>04/07/2013</date><title>fgj</title><description>fghj f</description></task><task><date>04/08/2013</date><title>test</title><description>swdefswde</description></task><task><date>04/09/2013</date><title>hj,h</title><description>hj,gh</description></task><task><date>04/16/2013</date><title>dfbd</title><description>dfbdfb</description></task><task><date>04/17/2013</date><title>dfb</title><description>dfb</description></task>
</calender>

有人可以帮我吗?

php xml simplexml
4个回答
3
投票

使用simplexml:->实时演示@ http://codepad.viper-7.com/Jl16Oh

$xml = simplexml_load_string($x); // XML is in $x

$today = date("m/d/Y");
$max = $xml->task->count()-1;

for ($i = $max; $i >=0; $i--) {

   if ($xml->task[$i]->date < $today) unset($xml->task[$i]);

}

1
投票

实际上非常棘手。您必须unset()适当的变量,但它必须是对父节点的引用(即unset($aTask)将不起作用)。另外,如果执行foreach ($xml_file->task as $index => $aTask)来跟踪子位置,则每次迭代都将获得task

您需要这样的难看代码:

$index = 0;
$remove = array();
foreach ($xml_file->task as $aTask) {
    if ($aTask->date < $today) {
        $remove[] = $index;
    }
    $index++;
}

foreach($remove as $index){
    unset($xml_file->task[0]);
}

P.S。如前所述,您的日期处理例程不起作用。

编辑:我实际上已经[[尝试我自己的代码和it works,所以下选民应该解释一下自己。


1
投票
我认为比较日期好像是已实现的数据类型是有问题的。

使用您的算法,您正在比较字符串,这不是您想要的。您可以使用preg_match来获取日,月和年,然后使用mktime来比较时间戳:

http://php.net/manual/en/function.preg-match.php

http://php.net/manual/en/function.mktime.php

EDIT:您甚至可以在没有preg_match的情况下执行此操作,这很耗时。这是将对象转换为数组的示例(您必须使用一种或另一种方法转换,也可以选择使用DOM,请参见:Remove a child with a specific attribute, in SimpleXML for PHP

<? $xml_file = (array)simplexml_load_file("file.xml")or die('Error reading XML file'); print_r($xml_file); $today=array( 'day' => intval(date('d')), 'month' => intval(date('m')), 'year' => intval(date('Y')), ); //print_r($today); foreach($xml_file['task'] as $key=>$aTask) { $date = (string)$aTask->date; $month = intval($date[0].$date[1]); $day = intval($date[3].$date[4]); $year = intval($date[6].$date[7].$date[8].$date[9]); //echo $aTask->title.': '.$month.'/'.$day.'/'.$year."\n"; if ( ($year < $today['year']) || ($year==$today['year'] && $month < $today['month']) || ($year==$today['year'] && $month == $today['month'] && $day < $today['day']) ) { //echo 'Deleting '.$aTask->title."\n"; unset($xml_file['task'][$key]); } } // Inspired from: https://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml $xml = '<?xml version="1.0"?>'."\n".'<calendar>'; function addChild($item) { global $xml; $xml.=$item->asXML(); } array_walk_recursive($xml_file['task'], 'addChild'); $xml .= '</calendar>'; //echo $xml; file_put_contents('out.xml', $xml); ?>

0
投票
我的XML和php:

<eventlist> <event type="dj"> <dj archive="0">msv3</dj> <calendar repeat="127"/> </event> <event type="dj"> <dj archive="0">msv5</dj> <calendar repeat="127"/> </event> <event type="dj"> <dj archive="0">msv8</dj> <calendar repeat="127"/> </event> </eventlist> <?php $xmlDoc = new DOMDocument(); $xmlDoc->load("calendar.xml"); $dj = $xmlDoc->documentElement; $djk = $dj->getElementsByTagName('dj'); foreach($djk as $dj2){ echo $dj2->nodeValue, PHP_EOL; } ?>

但是我如何删除msv5所在的位置?
© www.soinside.com 2019 - 2024. All rights reserved.