Search code examples
phpsimple-html-dom

How to imitate child selector with Simple HTML DOM?


Fellas!

I have one nasty page to parse but can't figure out how to extract correct data blocks from it using Simple HTML DOM, because it has no CSS child selector support.

HTML:

<ul class="ul-block">
   <li>xxx</li>
   <li>xxx</li>
   <li>
      <ul>
         <li>xxx2</li>
      </ul>
</ul>

How would I extract (direct) child li elements of parent ul.ul-block?

The $node->find('ul[class=ul-block] > li'); doesn't work and $node->find('ul[class=ul-block] li'); ofc finds also nested descandant li elements :(


Solution

  • Simple example with php DOM:

    $dom = new DomDocument;
    $dom->loadHtml('
    <ul class="ul-block">
       <li>a</li>
       <li>b</li>
       <li>
          <ul>
             <li>c</li>
          </ul>
       </li>
    </ul>
    ');
    
    $xpath = new DomXpath($dom);
    foreach ($xpath->query('//ul[@class="ul-block"]/li') as $liNode) {
        echo $liNode->nodeValue, '<br />';
    }