2011-08-12 57 views

回答

2

QueryPath附帶一個名爲QPXML的擴展,它具有多種附加方法。其中之一是comment()

要使用它,只需將其包含在你的腳本:

include 'QueryPath/QueryPath.php'; 
include 'QueryPath/Extensions/QPXML.php'; 

htmlqp($html, $selector)->comment(); 

這將檢索附加到當前選擇的節點(或多個)第一條評論。

如果你有一個非常複雜的一套都是一樣的節點中的註釋,你可以做這樣的事情:

$nodes = $qp->get(); 
foreach ($nodes as $node) { 
    foreach ($node->childNodes as $child) { 
     if ($child->nodeType == XML_COMMENT_NODE) { 
     // $child is a comment. 
     print $child->textContent; 
     } 
    } 
} 

這是有點難看,但它給其中一個元素都有的情況下更好地進入很多意見在裏面。

+0

謝謝!當我需要時,我應該在QP郵件列表上提問,幸運的是我找到了解決方法。 :) –

+0

我必須做'\ QueryPath :: enable('\ QueryPath \ Extension \ QPXML');'讓QPXML工作。 –

0

要通過的QueryPath獲得HTML頁面的所有評論:

function getAllComments($node) { 
     if ($node->hasChildNodes()) { 
      foreach ($node->childNodes as $child) { 
       $this->getAllComments($child); 
       if ($child->nodeType == XML_COMMENT_NODE) { 
        echo $child->textContent; 
       } 
      } 

     } 
    } 

    $html = $qp->get() ; 
    getAllComments($html[0]); 
相關問題