<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xml:base="http://www.example.com/feeds/events.xml" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:og="http://ogp.me/ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:sioc="http://rdfs.org/sioc/ns#" xmlns:sioct="http://rdfs.org/sioc/types#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
<channel>
<title>Event Calendar</title>
<link>http://www.example.com/feeds/events.xml</link>
<description></description>
<language>en</language>
<item>
<title>Thanksgiving Break 2015</title>
<link>http://www.example.com/event/42811211</link>
<description><div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>Happy Holidays.</p></div></div></div><div class="field field-name-field-date field-type-datetime field-label-inline clearfix"><div class="field-label">Date:&nbsp;</div><div class="field-items"><div class="field-item even"><span class="date-display-single" property="dc:date" datatype="xsd:dateTime" content="2015-11-25T00:00:00-05:00">Wednesday, November 25, 2015 (All day)</span></div><div class="field-item odd"><span class="date-display-single" property="dc:date" datatype="xsd:dateTime" content="2015-11-26T00:00:00-05:00">Thursday, November 26, 2015 (All day)</span></div><div class="field-item even"><span class="date-display-single" property="dc:date" datatype="xsd:dateTime" content="2015-11-27T00:00:00-05:00">Friday, November 27, 2015 (All day)</span></div></div></div><div class="field field-name-field-location field-type-text field-label-inline clearfix"><div class="field-label">Location:&nbsp;</div><div class="field-items"><div class="field-item even">Blacksburg, VA</div></div></div></description>
<pubDate>Wed, 19 Aug 2015 17:20:12 +0000</pubDate>
<dc:creator>Cronus</dc:creator>
<guid isPermaLink="false">311191 at http://www.example.com</guid>
<category domain="http://www.example.com/event-categories/othermiscellaneous">Other/Miscellaneous</category>
</item>
</channel>
</rss>
I am trying to extract all the elements embedded in the nested div tags in the XML above into an array such that all elements are extracted separately according to the type, eg: Date and Location. An ideal output would look like:
Title : Thanksgiving Break 2015
Link : http://www.example.com/event/42811211
Description : Happy Holidays.
Date : Wednesday, November 25, 2015 (All day),
Thursday, November 26, 2015 (All day),
Friday, November 27, 2015 (All day)
Location : Blacksburg, VA
I am new to php and DOM and I would sincerely appreciate help in this code. This is what I have so far
<?php
$rss = simplexml_load_file('http://www.example.com/feeds/events.xml');
$html = "";
$dom = new DOMDocument(); // the HTML parser used for descriptions' HTML
foreach ($rss->channel->item as $item) {
$title = $item->title;
$link = $item->link;
$description = $item->description;
foreach ($description as $desc)
{
$dom->loadHTML($desc);
$html = simplexml_import_dom($dom)->body;
// ?????
}
$html .= "Title : $title <br /> Link : $link <br /> Description : $description <br /> Date : <br /> Location : <hr>";
}
echo $html;
?>
Thanks in advance !