XML simpleDOM sortedXPath - How to select and display parent attribute?
문제
xml file :
<?xml version='1.0' encoding='utf-8'?>
<MyName>
<EventGroup Name="Japan Shows" ID="341">
<Event Name="Sapporo" ID="40163" StartTime="07/07/12 06:00:00" />
<Event Name="Consadole" ID="5106" StartTime="07/07/12 07:30:00" />
<Event Name="Albirex" ID="4063" StartTime="07/07/12 05:20:00" />
<Event Name="Niigata" ID="1144" StartTime="07/07/12 09:00:00" />
</EventGroup>
<EventGroup Name="USA Shows" ID="342">
<Event Name="Phoenix" ID="40162" StartTime="07/06/12 09:00:00" />
<Event Name="California" ID="5105" StartTime="07/08/12 08:30:00" />
<Event Name="Seattle" ID="4062" StartTime="07/08/12 02:20:00" />
<Event Name="Chicago" ID="1143" StartTime="07/08/12 04:00:00" />
</EventGroup>
</MyName>
code :
<?php
include 'SimpleDOM.php';
$xml = simpledom_load_file($cache_file);
$count = 0;
$max = 50;
foreach ($xml->sortedXPath("EventGroup/Event", "@StartTime", SORT_ASC) as $EventGroup => $item) {
$time = date("d/m - H:i", strtotime($item['StartTime']));
$event_name = $item['Name'];
if ($count < $max) {
echo "$time";
echo "$event_name";
}
$count++;
}
?>
Result :
07/06 - 09:00 Phoenix
07/07 - 06:00 Sapporo
07/07 - 07:30 Consadole
07/08 - 04:00 Chicago
etc...which are all correct.
But what about to show the approciate EventGroup Name? How i can select and display the parent name attribute to each event result? The result i desire should be like this :
USA Shows 07/06 - 09:00 Phoenix
Japan Shows 07/07 - 06:00 Sapporo
Japan Shows 07/07 - 07:30 Consadole
USA Shows 07/08 - 04:00 Chicago
etc...
Thanks in advance for any help!
해결책
You can get the parent of a node with parentNode()
. Then you can get the attribute value normally:
$parent = $item->parentNode();
echo $parent['Name'];
제휴하지 않습니다 StackOverflow