简体   繁体   English

一定时间后删除XML条目

[英]Deleting XML entries after certain amount of time

I have done a search but I can't really find anything related to what I am needing. 我已经进行了搜索,但找不到与我需要的东西有关的任何东西。 I am not sure if I am searching for it correctly or not. 我不确定是否要正确搜索它。 I have an XML file that is created via PHP and populated with data from a form. 我有一个通过PHP创建的XML文件,并用表单中的数据填充。 What I am trying to do is delete certain entries in that XML file after X amount of time. 我想做的是在X时间后删除该XML文件中的某些条目。 Is there an easy way to do this or can it even be done at all. 有没有简单的方法可以做到这一点,或者甚至可以做到? I was thinking of some php script that was run by a CRON to check the XML file and delete certain entries by the timestamp after X amount of time. 我在想由CRON运行的一些php脚本,用于检查XML文件并在X时间后的时间戳之前删除某些条目。 Can someone provide some suggestions or get me pointed in the right direction? 有人可以提供一些建议或让我指出正确的方向吗?

-Thanks! -谢谢!

To remove the old nodes from an XML file in a PHP script, you would need to parse the XML file, using, for example, the SimpleXML ( http://www.php.net/manual/en/simplexml.examples-basic.php ) library. 要从PHP脚本中的XML文件中删除旧节点,您需要使用例如SimpleXMLhttp://www.php.net/manual/zh/simplexml.examples-basic .php )库。

You can then check each of the nodes in question to see whether a timestamp attribute/child value is less than the current time - 12 hours, and if so, remove that node. 然后,您可以检查每个有问题的节点,以查看时间戳属性/子值是否小于当前时间-12小时,如果是,则删除该节点。

This assumes that the nodes have a timestamp attribute or value - you may need to add this in where you create the XML in PHP, for example 假定节点具有时间戳属性或值-例如,您可能需要在使用PHP创建XML的地方添加该属性或值。

<anode created="2013/06/04 12:00">
    //
</anode>

Lastly, to perform the date comparison, you will want to use an if statement similar to the following; 最后,要执行日期比较,您将需要使用类似于以下内容的if语句;

$timestamp = strtotime($domElement->getAttribute('timestamp'));
if ($timestamp < strtotime('now - 12 hour')) {
    // remove node
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM