PHP 5.6.0beta1 released

DomNode->append_sibling

(PHP 4 >= 4.2.0)

DomNode->append_sibling Adds new sibling to a node

Description

domelement DomNode->append_sibling ( domelement $newnode )

This functions appends a sibling to an existing node. The child can be created with e.g. domdocument_create_element(), domdocument_create_text() etc. or simply by using any other node.

Before a new sibling is added it is first duplicated. Therefore the new child is a completely new copy which can be modified without changing the node which was passed to this function. If the node passed has children itself, they will be duplicated as well, which makes it quite easy to duplicate large parts of an XML document. The return value is the added sibling. If you plan to do further modifications on the added sibling you must use the returned node.

This function has been added to provide the behaviour of domnode_append_child() as it works till PHP 4.2.

See also domnode_append_before().

add a note add a note

User Contributed Notes 1 note

up
0
s dot girard at pandora dot be
9 years ago
Small example on the use of domnode->append_sibling()
This function creates a news.xml file, that will be later on parsed with XSLT.

$doc = domxml_new_doc("1.0");
$root = $doc->create_element("rt");
$root = $doc->append_child($root);
$page = $doc->create_element("page");
$page = $root->append_child($page);
$page->set_attribute("pageimage","images/news.jpg");
   
while($row = mysql_fetch_row($result))
{
    $news = $doc->create_element("news");
    $news = $page->append_child($news);
       
    $topic = $doc->create_element("topic");
    $topic = $news->append_child($topic);
       
    $topic_content = $doc->create_text_node($row[0]);
    $topic_content = $topic->append_child($topic_content);
       
    $user = $doc->create_element("user");
    $user = $topic->append_sibling($user);
   
    $user_content = $doc->create_text_node($row[3]);
    $user_content = $user->append_child($user_content);
       
    $date = $doc->create_element("date");
    $date = $topic->append_sibling($date);
       
    $date_content = $doc->create_text_node($row[2]);
    $date_content = $date->append_child($date_content);
       
    $body = $doc->create_element("body");
    $body = $topic->append_sibling($body);
       
             $body_content = $doc->create_text_node($row[1]);
    $body_content = $body->append_child($body_content);
}
unlink("./data/news.xml");
$doc->dump_file("./data/news.xml");
To Top