簡體   English   中英

在PHP中從XML內部解析HTML標記

[英]Parsing HTML tags from inside XML in PHP

我正在嘗試使用simplexml_load_string創建自己的RSS提要(學習目的),同時在PHP中解析http://uk.news.yahoo.com/rss 我一直在閱讀<description>標簽內的HTML標簽。

到目前為止,我的代碼如下所示:

$feed = file_get_contents('http://uk.news.yahoo.com/rss');
$rss = simplexml_load_string($feed);

//for each element in the feed
foreach ($rss->channel->item as $item) {
    echo '<h3>'. $item->title . '</h3>'; 

        foreach($item->description as $desc){

             //how to read the href from the a tag???

             //this does not work at all
             $tags = $item->xpath('//a');
             foreach ($tags as $tag) {
                 echo $tag['href'];
             }
       }
}

有關如何提取每個HTML標記的任何想法?

謝謝

描述內容的特殊字符是編碼的,所以它不被視為XML中的節點,而只是一個字符串。 您可以解碼特殊字符,然后將HTML加載到DOMDocument中並執行您想要執行的任何操作。 例如:

foreach ($rss->channel->item as $item) {
    echo '<h3>'. $item->title . '</h3>'; 

        foreach($item->description as $desc){

            $dom = new DOMDocument();
            $dom->loadHTML(htmlspecialchars_decode((string)$desc));

            $anchors = $dom->getElementsByTagName('a');
            echo $anchors->item(0)->getAttribute('href');
        }
}

XPath也可用於DOMDocument,請參閱DOMXPath

RSS提要的<description>元素包含HTML。 如何如何使用SimpleXML解析XML的CDATA HTML內容? 您需要獲取該元素的節點值(HTML)並在addtional解析器中解析它。

鏈接問題的接受答案已經顯示出相當冗長,對於SimpleXML而言,無論RSS源是使用CDATA還是僅使用像您的情況那樣的實體,它在這里都不起作用。

$feed = file_get_contents('http://uk.news.yahoo.com/rss');
$rss  = simplexml_load_string($feed);
$dom  = new DOMDocument(); // the HTML parser used for descriptions' HTML

foreach ($rss->channel->item as $item)
{
    echo '<h3>' . $item->title . '</h3>', "\n";

    foreach ($item->description as $desc)
    {
        $dom->loadHTML($desc);

        $html = simplexml_import_dom($dom)->body;

        echo $html->p->a['href'], "\n";
    }
}

示例輸出:

...
<h3>Chantal nears hurricane strength in Caribbean</h3>
http://uk.news.yahoo.com/chantal-nears-hurricane-strength-caribbean-220149771.html
<h3>Placido Domingo In Hospital With Blood Clot</h3>
http://uk.news.yahoo.com/placido-domingo-hospital-blood-clot-215427742.html
<h3>Berlusconi's final tax fraud appeal hearing set for July 30</h3>
http://uk.news.yahoo.com/berlusconis-final-tax-fraud-appeal-hearing-set-july-214714122.html
<h3>China: Men Rescued From River Amid Floods</h3>
http://uk.news.yahoo.com/china-men-rescued-river-amid-floods-213005159.html
<h3>Snowden has not yet accepted asylum in Venezuela - WikiLeaks</h3>
http://uk.news.yahoo.com/snowden-not-yet-accepted-asylum-venezuela-wikileaks-190332291.html
<h3>Three US kidnap victims break silence</h3>
http://uk.news.yahoo.com/three-us-kidnap-victims-release-thankyou-video-093832611.html
...

希望這可以幫助。 與接受的答案相反,我認為沒有理由應用htmlspecialchars_decode ,實際上我很確定這會破壞事情。 此外,我的示例還展示了如何通過展示如何在解析HTML后將DOMNode轉換回SimpleXMLElement來保持SimpleXML訪問其他子節點的方式。

這里最好的做法是在$ item上使用var_dump()函數。

feed = file_get_contents('http://uk.news.yahoo.com/rss');
$rss = simplexml_load_string($feed);
foreach ($rss->channel->item as $item) {
    var_dump($item);
    exit;
}

一旦你這樣做,你會發現你所追求的價值被稱為“鏈接”。 因此,要打印出URL,您將使用以下代碼:

echo $item->link;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM