Jump to content

Finding attribute with simplexml


LLeoun

Recommended Posts

Hi all.

 

I'm trying to get the attibute beginTime form the xml below by doing:

$xml = simplexml_load_file("myxml.xml");
print $xml->BroadcastData->ScheduleData->ChannelPeriod->Event['beginTime'];
print $xml->BroadcastData->ScheduleData->ChannelPeriod->Event->Name;

 

This prints nothing, what am I doing wrong?

 

Here the xml:

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<BroadcastData>
    <ProviderInfo>
        <ProviderId>1</ProviderId>
        <ProviderName>the provider</ProviderName>
    </ProviderInfo>
    <ScheduleData>
        <ChannelPeriod endTime="2010-02-14T06:00:00+01:00" beginTime="2010-02-13T06:00:00+01:00">
            <ChannelId>1</ChannelId>

<Event EventId="3893" duration="7200" beginTime="2010-02-13T09:30:00+01:00">
                        <Description>Here a description</Description>
                        <Name>My title</Name>
</Event>
<Event EventId="3894" duration="1800" beginTime="2010-02-13T11:30:00+01:00">
                        <Description>Here a description 2</Description>
                        <Name>My title 2</Name>
</Event>
       
.
.
.

        </ChannelPeriod>
    </ScheduleData>
</BroadcastData>

 

Thanks for your help!

 

Link to comment
https://forums.phpfreaks.com/topic/193334-finding-attribute-with-simplexml/
Share on other sites

Use this function, it will return you object in array

function object2array($object)
{
   $return = NULL;
   if(is_array($object))
   {
	   foreach($object as $key => $value)
		   $return[$key] = $this->object2array($value);
   } 
   else 
   {
	   $var = get_object_vars($object);
	   if($var) 
	   {
		   foreach($var as $key => $value)
			   $return[$key] = ($key && !$value) ? NULL : $this->object2array($value);
	   } 
	   else return $object;
   }
   return $return;
}	

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.