我有充满图像的文件夹,这些图像被读作xml文件。 我想按照日期的子元素对它们进行分组。
图片格式:
entry_212_CA60CTF_2014-10-30_12-14-57.jpg
XML doc:
<?xml version="1.0"?>
<Incidents xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Count="3" Date="2014-10-30" Time="12:14:57" FileName="2014-10-30_dones_Hillsborough_Leisure_Centre.xml">
<Incident>
<EntryTime>2014-08-16T08:54:12</EntryTime>// want to group by this element
<ExitTime>2014-08-16T17:03:51</ExitTime>
</Incident>
PHP文件:
if(file_exists($xmlFullFilename)) {
$xmlDoc = new DomDocument();
$xmlDoc->load($xmlFullFilename);
$tmp = $xmlDoc->getElementsByTagName("Incidents");
$root = $tmp->item(0);
}
我想将同一日期的所有EntryTime分组到一个xml文件中。 xml文件应该只包含第16个日期。
eg:<EntryTime>2014-08-16T08:46:17</EntryTime>...
$tmp = split(" ", $entryTime);
$dateString = $tmp[0] . "T" . $tmp[1];
$entryTimeNode = $xmlDoc->createElement("EntryTime", $dateString);
答案 0 :(得分:0)
我认为您希望能够获取任何日期的信息,而不仅仅是8月16日,所以这里有一些更通用的代码可以帮助您。您可以使用DOMXPath从文件中提取所有日期/时间组合,创建一个包含不同日期的数组,然后获取该日期发生的所有事件。以下是一些示例代码:
$dom = new DomDocument();
$dom->loadXML( get_xml() );
$xp = new DOMXPath( $dom );
$d_list = array();
# get all EntryTime nodes in the document
foreach ($xp->query("Incident/EntryTime") as $x) {
# take a substring of the nodeValue corresponding to the date, put it in an array
# nodeValue is in the form YYYY-MM-DDTHH:MM:SS so we just need the YYYY-MM-DD part
$d_list[ substr( $x->nodeValue, 0, 10 ) ] = 1;
}
# $d_list now contains every date referenced in the file
# if you only want the Incidents for a certain date, you obviously don't need to use
# use a foreach loop!
foreach ($d_list as $d => $v) {
# select every EntryTime node where the value starts with $d
# the /.. at the end returns its parent node, i.e. the Incident node
foreach ($xp->query("Incident/EntryTime[starts-with(.,'$d')]/..") as $node) {
# now do whatever you want with the Incident node data
print_r($node);
}
}