Baigudin Software avatar
Baigudin Software logo
home
ru region
en region

PHP DOM Builder API 2.0

Baigudin Software registers the copyright of its own operating system – Embedded Object Operating System

The Russian Federal Service for Intellectual Property (Rospatent) registers the copyright of Embedded Object Operating System – Baigudin Software’s own embedded object-oriented real-time operating system.

Elements finding

Actually, DOM is hierarchical organization and one selecting criterion is not useful sometimes. What should we do if we want to find all A link tags with H2 header parent tags, or all links with HREF attributes which have the same values and are contained in DIV blocks? For this ways, we should use a find method.

Let's take previous article document example, here is it:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>PHP DOM Builder test page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
  </head>
  <body>
    <div id="content">
      <h1>
        Header
      </h1>
      <div class="article" id="id_article">
        <h2>
          Article 1
        </h2>
        <p>
          <a href="google.com">Text 1</a>
        </p>
      </div>
      <div class="article print">
        <h2>
          <a href="/article/">Article 2</a>
        </h2>
        <p>
          <a href="baigudin.software">Text 2</a>
        </p>
      </div>
    </div>
    <div id="footer">
      <p>
        &copy; copyright 2016
      </p>
    </div>
  </body>
</html> 

And try to find something:

<?php
$document
->find('#content');       // Find elements with id=content
$document->find('div#content');    // Find DIV with id=content
$document->find('.article');       // Find elements with class=article
$document->find('div.article');    // Find DIV elements with class=article
$document->find('.article.print'); // Find elements with class=article and print
$document->find('h2');             // Find H2 elements
$document->find('h1, h2');         // Find H1 and H2 elements
?> 

As you can see, query string corresponds to CSS Styling Tables. If we remember that DOM is hierarchical organization, we can find like this:

<?php
$document
->find('p a');      // Find A elements which are contained in P elements
$document->find('div h2 a'); // Find A elements in H2 elements which in DIV elements
?>

We can find elements by its attributes:

<?php
$document
->find('a[href]');                   // All links with href attribute
$document->find('a[href=baigudin.software]'); // All links with href=baigudin.software
?>

It is not difficult to understand that the find method finds child elements. So, parents method is exist too. Itfindsdirectparentelements.

<?php
// Find DIV elements with class=article which are parent of A elements
$document->find('a')->parents('div.article');
?>

Let's talk about a filtration. We have a filter and not methods. The filter method selects elements which matched by query, and the not method is contrary to it.

<?php
// Find DIV elements and filter which class=article
$document->find('div')->filter('.article');
// Find A elements and remove which href=baigudin.software
$document->find('a')->not('a[href=baigudin.software]');
?>

In the conclusion, we should say that all find, parents, filter, not methods return ElementList or ElementNode class objects which inherit the Element interface.

To the previous article

To the articles list

To the next article