這是我到目前爲止,我想添加日期/時間日誌到XML文件。XML搜索和addChild
<?php
// Load the XML file we want to write to
$visitors = simplexml_load_file("data.xml");
// Set e-mail xml search
$search_id = htmlentities($_POST['email']);
$visitors = $visitors->xpath("visitor[email='$search_id']");
// If we have a search result, email already exists in xml file
if(isset($visitors[0])){
$visitor = $visitors[0];
$email = (string) $visitor->email;
// ********** Need to add date/time for each visit here.
// So, I guess I need to search for e-mail address, then append to it.
// Help required here... and again below to confirm formatting for multiple
// dates.
} else {
$xml = simplexml_load_file("data.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$search_id = preg_replace('/[^(\x20-\x7F)]*/','', $search_id);
$visitor = $sxe->addChild("visitor");
$visitor->addChild("email", $search_id);
// ******** Not sure this is the correct xml formatting.
$date = $visitor->addChild('date');
$date->addChild("email", date("Y-m-d H:i:s"));
$sxe->asXML("data.xml");
} // if(isset($visitors[0])){
} // if(isset($data)){ ?>
產生
<?xml version="1.0" encoding="utf-8"?>
<visitors>
<visitor>
<email>[email protected]</email>
</visitor>
</visitors>
我想要做的就是添加日期日誌(incementing /追加),每次訪問。
所以結果會是(我不確定我的格式是否正確,哪些當然不起作用)。不要擔心日期/時間的PHP。
<?xml version="1.0" encoding="utf-8"?>
<visitors>
<visitor>
<email>[email protected]</email>
<date>2012-11-01 11:00:00</date>
<date>2012-11-02 14:00:00</date>
</visitor>
</visitors>
相關:[如何使用SimpleXML追加PHP的XML文件](http://stackoverflow.com/q/7098093/367456) – hakre