Re: How to parse this kind of XML
| From: | Nathan Nobbe | Date: | Thu, 07 Feb 2008 23:41:15 +0000 |
| Subject: | Re: How to parse this kind of XML | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-268915@lists.php.net to get a copy of this message | ||
On Feb 7, 2008 6:17 PM, VamVan <vamseevan@gmail.com> wrote:
> Hi,
>
> How can I parse this kind of XML data?
>
> <event:event>
>
>
> < event:sessionType>9</event:sessionType>
>
> <event:hostWebExID>marketingprograms</event:hostWebExID>
>
> <event:startDate>05/22/2008 09:00:00</event:startDate>
>
> <event:endDate>05/22/2008 10:00:00</event:endDate>
>
> <event:timeZoneID>4</event:timeZoneID>
>
> <event:duration>60</event:duration>
> </event:event>
>
> I am using SimpleXMLElement function but some cannot retrieve the
> nodes like timeZoneID for example
you will have to define a namespace in the root node of the xml document;
then use the SimpleXMLElement::children() method to fetch the nodes in
that namespace:
$xmlData =
<<<XMLDATA
<event xmlns:event="http://fake.com">
<event:sessionType>9</event:sessionType>
<event:hostWebExID>marketingprograms</event:hostWebExID>
<event:startDate>05/22/2008 09:00:00</event:startDate>
<event:endDate>05/22/2008 10:00:00</event:endDate>
<event:timeZoneID>4</event:timeZoneID>
<event:duration>60</event:duration>
</event>
XMLDATA;
$xml = new SimpleXMLElement($xmlData);
foreach($xml->children('http://fake.com') as $key
=> $val) {
echo "$key => $val" . PHP_EOL;
}
-nathan