Dom error

Discussion in 'PHP' started by ker, May 11, 2012.

  1. #1
    Hi all, I have a html page to parse, like this:
    
    <div class="rank pin">1</div>
    <div class="info">
    	<div class="clearfix">
    		<h3 class="business-name fn org">
    			<a href="/15100228?lid=171720240">asdf.</a>
    		</h3>
    	</div>
    <div class="thumbnail">
    	<a href="/15100228?lid=171720240">asdf.</a>
    </div>
    <div class="rank pin">2</div>
    <div class="info">
    	<div class="clearfix">
    		<h3 class="business-name fn org">
    			<a href="/15100228?lid=171720240">ghjk.</a>
    		</h3>
    	</div>
    <div class="thumbnail">
    	<a href="/15100228?lid=171720240">asdf.</a>
    </div>
    
    HTML:
    I want to get data from e.g. .info -> .clearfix -> .business-name fn org -> a.

    When I run
    foreach(@$dom->getElementsByTagName("div") as $div)
    {
    	if($div->hasAttributes() && $div->attributes->getNamedItem("class") && $div->attributes->getNamedItem("class")->textContent == "rank pin")
    	{
    		echo $div->textContent."<br/>";
    		echo $div->nextSibling->textContent;
    	}	
    }
    PHP:
    I only get contents for $div->textContent but not for $div->nextSibling->textContent.
    I also tried $div->nextSibling->firstChild->firstChild->firstChild->textContent and $div->nextSibling->firstChild->textContent but nothing worked.
    Help, please :)
     
    ker, May 11, 2012 IP
  2. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #2
    Use DomXPath in order to fetch information from the dom
    http://php.net/manual/en/class.domxpath.php
    http://www.php.net/manual/en/domxpath.query.php

    $domxpath = new DOMXPath($dom);
    
    $domNodeList = $domxpath->query("//div/div/h3/a");
    //iterate thru your results....
    
    
    PHP:
    I havent tried this code, since playing with PHP dom is quite painful, since print_r($dom) does not produce results.....
    You would be better of using on each element with the following code:

    $xml = $domNode->saveXML();  //maybe there is saveHtml() method too, but xml and html are similar anyway.
    
    
    PHP:
    Then you can inspect the contents of each node.
     
    e-abi, May 11, 2012 IP