dongpiaozhao6836 2015-11-27 06:36
浏览 56

使用DOM检索嵌套div标签中的所有元素

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

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 !

  • 写回答

2条回答 默认 最新

  • dtcd27183 2015-11-27 06:58
    关注

    Try This:

    $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;
                 }   
    
        }
    

    or

    $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)."
    ";
    
    echo $result;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)