Baigudin Software avatar
Baigudin Software logo
ru region
en region

PHP DOM Builder API 2.0

Baigudin Software participates in OS DAY 2017 conference

The Baigudin Software Company has participated in OS DAY – a fourth scientific-practical conference, which aims to determine a mission of Russia in developing sphere of operating systems.

Elements copying

Until this instant, we talked about the elements searching for adding new, or changing those attributes. Sometimes, we need to copy elements. Let’s take an example, which was used by us, and remove the second block with “article” class attribute value. So, we will try to reconstruct removed block by using the first one block.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" xml:lang="en" lang="en">
    <title>PHP DOM Builder test page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
    <div id="content">
      <div class="article" id="id_article">
          Article 1
          <a href="">Text 1</a>
      <!-- We need to reconstruct this one
      <div class="article print">
          <a href="/article/">Article 2</a>
          <a href="">Text 2</a>

    <div id="footer">
        &copy; copyright 2016

Before we will proceed to copy we want to note about basic rules of PHP language and DOM with reference to our library.

We told that every  HTML document could be represented as tree list with elements. In our case, the elements are objects of Element class. When we call the create method, we create a root element which is not liked to any trees. When we call some searching method, we get some objects which are linked to document tree.

As we know, PHP language classes  are passed by references. So, when we select some elements from a tree, we get references to those elements. All methods for new element creating receive a reference to ElementNode or ElementList class as argument. When the library gets that type of argument, it checks that for linking to other trees. If argument is linked, library clones it and all its child elements. Accordingly, library has gotten a copy of given branch and links coped branch to corresponding place of tree after that.

Usually, some elements on tree have some attributes which affect on outputted HTML document. If we want to get a valid HTML document, some attributes value must be unique for it. For example, a value of "id" or "name" attribute must be only one in a document . When library clones elements, it does not check  these factors. Thus, we can have a document with equal values of critical attributes for validating.

Let's try to allow what we were talking about, and copy a branch beginning of the DIV tag with "id_article" value of ID attribute.

// Find the source element
$div $document->getElementById('id_article');
// Clone found element to the tree after itself and get cloned branch
$div $div->after($div);
// Change attributes of DIV element
// Change contents of H2 element
$div->find('h2')->insert('a')->href('/article/')->html('Article 2');
// Change contents of P element
$div->find('p a')->href('')->html('Text 2');

We hope you have understood what will be as the result of this script executing. And in the conclusion of this article, we want to say about removing. Any elements of tree can be removed by using a remove method of the library. The method unlinks a removing element from tree and returns it with whole chain of child elements. So the removed element becomes a root.

To the previous article

To the articles list

To the next article