简体   繁体   中英

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. What I am trying to do is delete certain entries in that XML file after X amount of time. 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. 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.

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.

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

<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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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