2013-04-23 74 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文档格式错误,即它缺少根节点。