The revamped PHP 4.3 DOM XML functions follow a pattern. You create
an object as either an element or a text node, add and set any
attributes you want, and then append it to the tree in the spot it
belongs.
You can also append a new child element to $book.
Since $book is a child of $dom,
the new element is, by extension, a grandchild of
$dom:
$title_element = $dom->create_element('title');
$title = $book->append_child($title_element);
By calling $book->append_child( ), this code
places the $title_element element under the
$book element.
$text_node = $dom->create_text_node('PHP Cookbook');
$title->append_child($text_node);
Since $title is already added to the document,
there's no need to reappend it to
$book.
The order in which you append children to nodes
isn't important. The following four lines, which
first append the text node to $title_element and
then to $book, are equivalent to the previous
code:
$title_element = $dom->create_element('title');
$text_node = $dom->create_text_node('PHP Cookbook');
$title_element->append_child($text_node);
$book->append_child($title_element);