Baigudin Software avatar
Baigudin Software logo
home
ru region
en region

PHP DOM Builder API

BOOS Core has been released as version 2.1

The new release of BOOS Core has gotten ARM architecture with instruction set version 5, some independent algorithms of any compilers, root interface for all interfaces, and many tiny aspects, which were implemented in it.

Elements fetching

These methods are used for fetching elements of a document:

  • getElementsById.
  • getElementsByTagName.
  • getElementsByClassName.
  • getElementsByAttr.
  • getParentsById.
  • getParentsByTagName.
  • getParentsByClassName.
  • getParentsByAttr.

You can guess about criterions which are used for fetching by names of these methods. Methods with getElements prefix search child elements, and getParents prefix searches parent elements.

Let's generate the document for example:

<?php
use DomBuilder\Element as Element;
// Create root element of document
$document Element::create();
// Create document 
$document->insert('html')
  ->
insert('head')
    ->
insert('title')->html('PHP DOM Builder test page')
    ->
after('meta')
    ->
attr('http-equiv''Content-Type')
    ->
attr('content''text/html; charset=utf-8')
    ->
parent()
  ->
after('body')
    ->
insert('div')->attr('id''content')
      ->
insert('h1')->html('Header')
      ->
after('div')->addClass('article')->attr('id''id_article')
        ->
insert('h2')->html('Article 1')
        ->
after('p')
          ->
insert('a')->href('google.com')->html('Text 1')
          ->
parent()          
        ->
parent()
      ->
after('div')->addClass('article')->addClass('print')
        ->
insert('h2')
          ->
insert('a')->href('/article/')->html('Article 2')
          ->
parent()
        ->
after('p')
          ->
insert('a')->href('baigudin.software')->html('Text 2')
          ->
parent()
        ->
parent()        
      ->
parent()        
    ->
after('div')->attr('id''footer')
      ->
insert('p')->html('&copy; copyright 2016');
?>

If you difficultly image the generated HTML document after outputting, 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>

Let's try to find something and change it:

<?php
// Change H2 header for article 1
$h2 $document->getElementsById('id_article')->get(0)
    ->
child()->html('New article 1');
// Remove ID attribute for parents DIV
$h2->getParentsByTagName('div')->removeAttr('id');
// Find elements with ID attribute values equal "content"
$len $document->getElementsByAttr('id''content')->length();
if(
$len == 0)
{
  
// Find and remove elements with CLASS attribute value equal "print"
  
$document->getElementsByClassName('print')->remove();
}

// Output document
echo Element::getDocument($document);
?>

For the beginning, we change the H2 header of the first article. We will search it by "id_article" identifier by using getElementsById method. Elements fetching methods return a reference to ElementList class. If we want to get an element, we should call a get method with zero as a parameter which returns the first element of the list. As the result, we get DIV tag with "id_article" identifier. The first child element of that operation is H2 tag which we wanted to change and change it.

Note: HTML document is valid if it has one unique identifier of whole document. So we understand that errors are occurred. That is why we have implemented the getElementsById method. If you are sure in your document, you can use getElementById method. It returns an element of ElementNode class if it has found only one element, or false.

We have found H2 tag. Let's find all parents DIV tags for this tag and remove its ID attributes. It is not difficult to understand that our document will not have ID attributes with "id_article" and "content" values. Try to check it. Find an element with "content" value of ID attribute. If we do not find it, we will remove all elements with "print" value of class attribute. Here is the result:

<!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>
      <h1>
        Header
      </h1>
      <div class="article">
        <h2>
          New article 1
        </h2>
        <p>
          <a href="google.com">Text 1</a>
        </p>
      </div>
    </div>
    <div id="footer">
      <p>
        &copy; copyright 2016
      </p>
    </div>
  </body>
</html> 

Naturally, our example does not have practical sense, but we wanted to show some possibilities for you. In the next article, we will consider an elements finding by using some criterions.

To the previous article

To the articles list

To the next article