Skip to content

Getting Started: 2. Adding Nodes

Daniele Orlando edited this page Feb 21, 2016 · 2 revisions

Adding a node is super easy. Because FluidXML implements the fluent OOP pattern, multiple operations can be performed chaining methods calls.

Concise syntax

$book->add('title',  'The Theory Of Everything')
     ->add('author', 'S. Hawking')
     ->add('description');

Extended syntax

$book->addChild('title',  'The Theory Of Everything')
     ->addChild('author', 'S. Hawking')
     ->addChild('description');
echo $book;
<?xml version="1.0" encoding="UTF-8"?>
<book>
  <title>The Theory Of Everything</title>
  <author>S. Hawking</author>
  <description/>
</book>

The add()/addChild() method supports up to four arguments to achieve from the simplest node insertion to nested trees creation.

Public API

->add/addChild($node, $value?, $attributes? = [], $switchContext? = false)

Pro Tip:
Except for the $node argument, all others arguments can be passed in any order.

One of the most important argument is the boolean flag $switchContext. Passing a true boolean value returns the new node instead of the current one.

Concise syntax

$book->add('chapters', true)
         ->add('chapter', 'Ideas About The Universe')
         ->add('chapter', 'The Expanding Universe');

// true asks to return the 'chapters' node instead of the 'book' node.

Extended syntax

$book->addChild('chapters', true)
         ->addChild('chapter', 'Ideas About The Universe')
         ->addChild('chapter', 'The Expanding Universe');

// true asks to return the 'chapters' node instead of the 'book' node.
echo $book;
<?xml version="1.0" encoding="UTF-8"?>
<book>
  <title>The Theory Of Everything</title>
  <author>S. Hawking</author>
  <description/>
  <chapters>
    <chapter>Ideas About The Universe</chapter>
    <chapter>The Expanding Universe</chapter>
  </chapters>
</book>

Chaining methods calls is nice, but sometimes it's more convenient creating nodes in a batch operation, for example when the nodes' structure is defined using an array. To demonstrate this concept, we create a new document that will be filled with food.

Concise syntax

$food = fluidxml('food');

$food->add('fruit')               // A 'fruit' node with an empty content.
     ->add('fruit', 'orange');    // A 'fruit' node with 'orange' as content.


// Batch insertion of nodes.

$food->add([ 'cake'  => 'Tiramisu',
             'pizza' => 'Margherita' ]);


// PHP arrays can't contain identical keys.
// But it's still possible to create, in a batch operation, nodes with the same tag.

$food->add([ [ 'pasta' => 'Carbonara' ],
             [ 'pasta' => 'Matriciana' ] ]);

Extended syntax

$food = new FluidXml('food');

$food->addChild('fruit')               // A 'fruit' node with an empty content.
     ->addChild('fruit', 'orange');    // A 'fruit' node with 'orange' as content.


// Batch insertion of nodes.

$food->addChild([ 'cake'  => 'Tiramisu',
                  'pizza' => 'Margherita' ]);


// PHP arrays can't contain identical keys.
// But it's still possible to create, in a batch operation, nodes with the same tag.

$food->addChild([ [ 'pasta' => 'Carbonara' ],
                  [ 'pasta' => 'Matriciana' ] ]);
<?xml version="1.0" encoding="UTF-8"?>
<food>
  <fruit/>
  <fruit>orange</fruit>
  <cake>Tiramisu</cake>
  <pizza>Margherita</pizza>
  <pasta>Carbonara</pasta>
  <pasta>Matriciana</pasta>
</food>

Another important argument is $attributes, which allows to set the attributes of a node contextually to its creation.

Concise syntax

$food->add('fruit', 'apple', ['color' => 'red']);

Extended syntax

$food->addChild('fruit', 'apple', ['color' => 'red']);

Which is identical to

Concise syntax

$food->add('fruit', 'apple', true)        // Remember, passing 'true' returns the created node.
         ->attr([ 'color' => 'red' ]);

Extended syntax

$food->addChild('fruit', 'apple', true)        // Remember, passing 'true' returns the created node.
         ->setAttribute([ 'color' => 'red' ]);

The advantage comes when multiple nodes have the same attributes.

Concise syntax

$food->add([ ['egg'], ['egg'] ], ['price' => '0.25']);

// A bunch of eggs all with the same price.

Extended syntax

$food->addChild([ ['egg'], ['egg'] ], ['price' => '0.25']);

// A bunch of eggs all with the same price.

Creating arbitrarily complex structures is possible too nesting arrays and using the @ special syntax. An array key starting with an @ is interpreted as an attribute identifier and an array key equal to @ is interpreted as the text content.

Concise syntax

$food->add([ 'menu' => [
                 'pasta' => [
                     'spaghetti' => [
                         '@id'      => '123',
                         '@country' => 'Italy',
                         '@'        => 'Spaghetti are an Italian dish...',

                         'variants' => [
                             'tomato' => [ '@type' => 'vegan' ],
                             'egg'    => [ '@type' => 'vegetarian' ] ]]]]]);

Extended syntax

$food->addChild([ 'menu' => [
                     'pasta' => [
                         'spaghetti' => [
                             '@id'      => '123',
                             '@country' => 'Italy',
                             '@'        => 'Spaghetti are an Italian dish...',

                             'variants' => [
                                 'tomato' => [ '@type' => 'vegan' ],
                                 'egg'    => [ '@type' => 'vegetarian' ] ]]]]]);
echo $food;
<?xml version="1.0" encoding="UTF-8"?>
<food>
  <fruit/>
  <fruit>orange</fruit>
  <cake>Tiramisu</cake>
  <pizza>Margherita</pizza>
  <pasta>Carbonara</pasta>
  <pasta>Matriciana</pasta>
  <fruit color="red">apple</fruit>
  <egg price="0.25"/>
  <egg price="0.25"/>
  <menu>
      <pasta>
        <spaghetti id="123" country="Italy">
            Spaghetti are an Italian dish...
            <variants>
                <tomato type="vegan"/>
                <egg type="vegetarian"/>
            </variants>
        </spaghetti>
      </pasta>
    </menu>
</food>

3. Adding XML Fragments 〉