2015-11-27 3 views
0
<?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>&lt;div class=&quot;field field-name-body field-type-text-with-summary field-label-hidden&quot;&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot; property=&quot;content:encoded&quot;&gt;&lt;p&gt;Happy Holidays.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;field field-name-field-date field-type-datetime field-label-inline clearfix&quot;&gt;&lt;div class=&quot;field-label&quot;&gt;Date:&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;span class=&quot;date-display-single&quot; property=&quot;dc:date&quot; datatype=&quot;xsd:dateTime&quot; content=&quot;2015-11-25T00:00:00-05:00&quot;&gt;Wednesday, November 25, 2015 (All day)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;field-item odd&quot;&gt;&lt;span class=&quot;date-display-single&quot; property=&quot;dc:date&quot; datatype=&quot;xsd:dateTime&quot; content=&quot;2015-11-26T00:00:00-05:00&quot;&gt;Thursday, November 26, 2015 (All day)&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;field-item even&quot;&gt;&lt;span class=&quot;date-display-single&quot; property=&quot;dc:date&quot; datatype=&quot;xsd:dateTime&quot; content=&quot;2015-11-27T00:00:00-05:00&quot;&gt;Friday, November 27, 2015 (All day)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class=&quot;field field-name-field-location field-type-text field-label-inline clearfix&quot;&gt;&lt;div class=&quot;field-label&quot;&gt;Location:&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;field-items&quot;&gt;&lt;div class=&quot;field-item even&quot;&gt;Blacksburg, VA&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</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> 

Я пытаюсь извлечь все элементы, встроенные в вложенных DIV тегов в XML выше в массив таким образом, что все элементы извлекаются отдельно в зависимости от типа, например: Дата и местоположение. Идеальный выход будет выглядеть так:Получить все элементы вложенных DIV тегов с помощью DOM

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 

Я новичок в PHP и DOM, и я искренне ценю помощь в этом коде. Это то, что у меня есть до сих пор

<?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; 

?> 

Заранее благодарен!

ответ

0

Попробуйте:

$xmlfile='http://www.example.com/feeds/events.xml'; 
    $xml = simplexml_load_file($xmlfile) or die("Error: Cannot create object"); 
    foreach($xml->children() as $item) 
    { 
    $title   = $item->title; 
    $link   = $item->link; 
    $description = $item->description; 
    $description = $item->description; 

      foreach ($description as $desc) 
      { 
       $title1   = $desc->title; 
       $link 1   = $desc->link; 
       $description1 = $desc->description; 
      } 

    } 

или

$document = new DOMDocument(); 
$document->load($xmlfile); 

// this will also output doctype and comments at top level 
foreach($document->childNodes as $node) 
    $result .= $document->saveXML($node)."\n"; 

echo $result; 
} 
+0

Привет. Спасибо за ответ. Я не вижу, как этот код достигнет конечного результата. Код здесь не учитывает вложенные теги div, что является основной проблемой здесь. Не могли бы вы поделиться своей логикой? – user2060870

+0

Попробуйте второй сегмент. –

0

Использование simplexml, DOMDocument и XPath

$rss = simplexml_load_file('http://www.example.com/feeds/events.xml'); 

foreach($rss->channel->item as $item){ 

    print 'Title: ' . $item->title . PHP_EOL; 
    print 'Link: ' . $item->link . PHP_EOL; 

    $dom = new DOMDocument(); 
    $dom->loadHTML($item->description); 

    $xpath = new DOMXpath($dom); 

    $description = $xpath->query("//div[contains(@class, 'field-name-body')]"); 
    print 'Description: ' . $description->item(0)->nodeValue . PHP_EOL; 

    $date = $xpath->query("//div[contains(@class, 'field-name-field-date')]"); 
    print 'Date: ' . $date->item(0)->nodeValue . PHP_EOL; 

    $location = $xpath->query("//div[contains(@class, 'field-name-field-location')]/div/div"); 
    print 'Location: ' . $location->item(0)->nodeValue . PHP_EOL; 
} 
/* 
will output 

Title: Thanksgiving Break 2015 
Link: http://www.example.com/event/42811211 
Description: Happy Holidays. 
Date: Date: Wednesday, November 25, 2015 (All day)Thursday, November 26, 2015 (All day)Friday, November 27, 2015 (All day) 
Location: Blacksburg, VA 

*/ 
+0

Это сработало! Огромное спасибо. – user2060870

+0

Добро пожаловать! –