2013-06-29 29 views
0

我想通過加載一個簡單的URL作爲DOM對象並使用xPath查詢DOM來學習如何使用xPath和PHP。我一直在遇到很多麻煩,並試圖排除這些警告消息很長一段時間,但沒有成功。獲取大量警告的基本xPath

我很感激任何建議,爲什麼發生這種情況。

非常感謝提前!

PHP

<?php 
$doc = new DOMDocument(); 
$doc->loadHTMLFile("http://n5a.software.com/calendar/da2013/profile.php?__id=691"); 

$xpath = new DOMXpath($doc); 

$elements = $xpath->query("//ul[@class='flper']/li[2]"); 

if (!is_null($elements)) { 
    foreach ($elements as $element) { 
     $nodes = $element->childNodes; 
     foreach ($nodes as $node) { 
      echo $node->nodeValue. "\n"; 
     } 
    } 
} 

?> 

輸出

Warning: DOMDocument::loadHTMLFile(): Attribute style redefined in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 92 in C:\xampp\htdocs\www\test\xpath.php on line 3 

Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: expecting ';' in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 101 in C:\xampp\htdocs\www\test\xpath.php on line 3 

Warning: DOMDocument::loadHTMLFile(): Attribute cellspacing redefined in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 109 in C:\xampp\htdocs\www\test\xpath.php on line 3 

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : span in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 165 in C:\xampp\htdocs\www\test\xpath.php on line 3 

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : script in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 166 in C:\xampp\htdocs\www\test\xpath.php on line 3 

Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: no name in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 197 in C:\xampp\htdocs\www\test\xpath.php on line 3 

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : td in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 205 in C:\xampp\htdocs\www\test\xpath.php on line 3 

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : tr in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 230 in C:\xampp\htdocs\www\test\xpath.php on line 3 

Warning: DOMDocument::loadHTMLFile(): Unexpected end tag : table in http://n5a.software.com/calendar/da2013/profile.php?__id=691, line: 231 in C:\xampp\htdocs\www\test\xpath.php on line 3 

回答

3

禁用警告與libxml_use_internal_errors(true) http://www.php.net/manual/en/function.libxml-use-internal-errors.php

它是畸形的HTML,沒有什麼可以真正做些什麼,如果你不控制HTML。

+0

感謝您的回覆。這樣擺脫了大部分的警告,但是,我仍然得到這個:'警告:DOMDocument :: loadHTMLFile():作爲輸入提供的空字符串作爲輸入提供在C:\ xampp \ htdocs \ www \ test \ xpathgenius.php在線15'這是爲什麼? – AnchovyLegend

+0

loadHTMLFile()接受來自輸入文件的URL stings,並且我仔細檢查了是否沒有任何可能被誤認爲URL的過多新行... – AnchovyLegend

+0

哪一行是行15?可能不是最後的大括號。 –