2013-04-23 72 views
0

我有domDocumet一個非常簡單的代碼,但它有一個錯誤,我解決不了:DOM文檔提取數據

function getTagXML($mensaje, $tagname){ 

      $dom = new domDocument('1.0', 'UTF-8'); 
      libxml_use_internal_errors(true); 
      // load the html into the object ***/ 
      $dom->loadHTML($mensaje); 

      //discard white space 
      $dom->preserveWhiteSpace = false; 
      $nodeList= $dom->getElementsByTagName($tagname); // here u use your desired tag  
      $node = $nodeList->item(0); 

      $item = trim($node->nodeValue); 
      libxml_clear_errors(); 

      return $item; 
} 

我得到了錯誤:

Notice: Trying to get property of non-object in line 82: 

線82:

$item = trim($node->nodeValue); 

回答

1

錯誤消息意味着$nodeList不是一個對象,這意味着$dom->getElementsByTagName($tagname)返回NULL

基於your other question我會假設發生這種情況是因爲您的XML文檔格式錯誤,即它缺少根節點。