Baigudin Software avatar
Baigudin Software logo
home
ru region
en region

PHP DOM Builder API

We have participated 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.

DOM tree traversal

In previous article, we have created a basic HTML document. Actually, it was only a cover for future events. Let's try to add something more than we have. For example, we will output "Hello world" and add "PHP DOM Builder test page" title.

<?php
use DomBuilder\Element as Element
// Create document 
$document Element::create()   
  ->
insert('html')     
  ->
insert('head')     
  ->
after('body')     
  ->
root(); 
// Look for BODY tag in document  
$p $document->child()->child()->next()->insert('p')->html('Hello world'); 
// Look for HEAD tag relatively from created P teg
$p->parent()->prev()->insert('title')->html('PHP DOM Builder test page'); 
// Output document 
echo Element::getDocument($document);
?>

The result of this example is this document page:

<!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>
  </head>
  <body>
    <p>
      Hello world
    </p>
  </body>
</html>

That tree traversal technique is more efficient, but it is not useful some times. Other techniques for selecting elements, we will discuss in next articles .

To the previous article

To the articles list

To the next article