PHP and javascript snippets you can copy and paste.

Wednesday, April 4, 2007

xml_replace_nodeContent( &$node, &$new_content )

/**
* Replaces node contents.
*
* Needed as a workaround for bug/feature of set_content.
* This version puts the content
* as the first child of the new node.
* If you need it somewhere else, simply
* move $newnode->set_content() where
* you want it.
* Credit: http://www.php.net/manual/en/function.domnode-set-content.php
*/

function xml_replace_nodeContent( &$node, &$new_content )
{

$dom =& $node->owner_document();

$newnode =& $dom->create_element( $node->tagname );

$newnode->set_content( $new_content );

$atts =& $node->attributes();
foreach ( $atts as $att )
{
$newnode->set_attribute( $att->name, $att->value );
}


$kids =& $node->child_nodes();
foreach ( $kids as $kid )
{
if ( $kid->node_type() != XML_TEXT_NODE )
{
$newnode->append_child( $kid );
}
}

return $newnode;

}

No comments: