用PHP读取XML - 属性问题

时间:2014-03-27 12:30:54

标签: php xml simplexml

我有以下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);

            }
        }
}

?>

出于某种原因,每个名称都会重复显示显示属性开始时间的循环,这是我执行脚本时的示例:

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

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

如上所示,重复开始时间并且与xml文件不匹配。

1 个答案:

答案 0 :(得分:2)

尝试这样:

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>