1

xml文件:

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

代码 :

<?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++;
}
?>

结果 :

07/06 - 09:00 Phoenix
07/07 - 06:00 Sapporo
07/07 - 07:30 Consadole
07/08 - 04:00 Chicago

等等……这些都是正确的。

但是如何显示适当的 EventGroup 名称?我如何选择并显示每个事件结果的父名称属性?我想要的结果应该是这样的:

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

提前感谢您的帮助!

4

1 回答 1

1

您可以使用parentNode(). 然后就可以正常获取属性值了:

$parent = $item->parentNode();
echo $parent['Name'];
于 2012-07-05T21:01:25.617 回答