用PHP读取XML -属性问题


Reading XML with PHP - Attribute Issue

我有下面的XML示例文件,我试图使用PHP显示:

<?xml version="1.0" encoding="UTF-8" ?>
<BroadcastData creationDate="20140326085217">
<ScheduleData>
<ChannelPeriod beginTime="20140326090000" endTime="20140402044500">
<ChannelId>Rai Uno</ChannelId>
<Event beginTime="20140326090000" duration="1800">
<EventId>260852180006</EventId>
<EventType>P</EventType>
<EpgProduction>
<EpgText language="eng">
<Name>Unomattina storie vere</Name>
</EpgText>
</EpgProduction>
</Event>
<Event beginTime="20140326093000" duration="1500">
<EventId>260852180007</EventId>
<EventType>P</EventType>
<EpgProduction>
<EpgText language="eng">
<Name>Unomattina Verde</Name>
</EpgText>
</EpgProduction>
</Event>
下面是解析XML的PHP代码:
// Create connection
$con=mysqli_connect("localhost","test","test","epg");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$completeurl ='test.xml';
$doc = simplexml_load_file($completeurl);
foreach ($doc->ScheduleData->ChannelPeriod as $channelPeriod) {
    $channelId = $channelPeriod->ChannelId;
    foreach ($channelPeriod->Event as $event) {
        $beginTime = $event->['beginTime'];
    foreach ($channelPeriod->Event as $name) {
        $programName = $name->EpgProduction->EpgText->Name;
        printf('<p>Channel: %s<br />Begin Time: %s<br />Program Name: %s</p>', $channelId, $beginTime, $programName);
            }
        }
}
?>

由于某些原因,显示属性begin time的循环对每个名称重复显示,这是我执行脚本时的示例:

Channel: Rai Uno
Begin Time: 260852180006
Program Name: Unomattina storie vere
Channel: Rai Uno
Begin Time: 260852180006
Program Name: Unomattina Verde

正如您在上面看到的,开始时间是重复的,并且与xml文件不匹配。

试试:

foreach ( $doc->ScheduleData->ChannelPeriod as $channelPeriod )
{
   $channelId = $channelPeriod->ChannelId;
   foreach ( $channelPeriod->Event as $event )
   {
      $beginTime = $event['beginTime'];
      $programName = $event->EpgProduction->EpgText->Name;
      printf( '<p>Channel: %s<br />Begin Time: %s<br />Program Name: %s</p>', $channelId, $beginTime, $programName );
   }
}

输出:

Channel: Rai Uno
Begin Time: 20140326090000
Program Name: Unomattina storie vere
Channel: Rai Uno
Begin Time: 20140326093000
Program Name: Unomattina Verde

同时,你的xml应该是这样的:

<?xml version="1.0" encoding="UTF-8" ?>
<BroadcastData creationDate="20140326085217">
   <ScheduleData>
      <ChannelPeriod beginTime="20140326090000" endTime="20140402044500">
         <ChannelId>Rai Uno</ChannelId>
         <Event beginTime="20140326090000" duration="1800">
            <EventId>260852180006</EventId>
            <EventType>P</EventType>
            <EpgProduction>
               <EpgText language="eng">
                  <Name>Unomattina storie vere</Name>
               </EpgText>
            </EpgProduction>
         </Event>
         <Event beginTime="20140326093000" duration="1500">
            <EventId>260852180007</EventId>
            <EventType>P</EventType>
            <EpgProduction>
               <EpgText language="eng">
                  <Name>Unomattina Verde</Name>
               </EpgText>
            </EpgProduction>
         </Event>
      </ChannelPeriod>
   </ScheduleData>
</BroadcastData>