Baigudin Software avatar
Baigudin Software logo
home
ru region
en region

PHP DOM Builder API 2.0

Baigudin Software is unveiled in global edition

Baigudin Software project has been unveiled in English global edition and it always accessed by www.baigudin.software/en/.

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