Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : DOM XML Functions : DomNode->set_content

DomNode->set_content

Sets content of node ()
bool DomNode->set_content ( string content )

Warning:

This function is currently not documented; only the argument list is available.

Code Examples / Notes » domnode_set_content

robinson

If you want to set_content which is not in Alphabet, such as big5 charset, you have to use the iconv() to convert your codeset from big5 to something else, such as UTF-8. Here is a example:
$node->set_content(iconv("big5","UTF-8","This is a test.");
$node->set_content(iconv("big5","UTF-8","[Chinese Chars]");
These works find that won't dump garbage chars when your use dump_mem to file.
Ps. don't forget to include the php_iconv.dll


fivebull

if you had used set_name() to change the node's tag name :
$newnode =& $dom->create_element( $node->tagname );
should be:
$newnode =& $dom->create_element(  $node->node_name());


dh

function replace_content( &$node, $new_content )
{
$dom = &$node->owner_document();
$kids = &$node->child_nodes();
foreach ( $kids as $kid )
if ( $kid->node_type() == XML_TEXT_NODE )
$node->remove_child ($kid);
$node->set_content($new_content);
}


blah

For those transitioning to PHP5 DOM XML code, there is a gotcha with this function.
In PHP4, when you call $node->set_content($string), the $string must have special HTML characters encoded. The method does not properly handle the "&" character, so this is required to properly set the content
In PHP5, when you call $node->appendChild($dom->createTextNode($string)), PHP will properly encode the "&" character without the need for HTML encoding.
Because of this, older code that is currently using htmlspecialchars() in the set_content() call will potentially double-encode the "&" character if you keep the call to htmlspecialchars() in the createTextNode() call.


kdan

As of v4.2.2 of PHP, with libxml version 2.4.19, set_content does not replace the content of the node, but only appends to it. Bug, feature, who knows!
To replace the content of a node, create a new node, copy the properties and children of the old node to the new one, then set the content of the new one and use replace_node to put it back into the DoM.
Daniel


28-jun-2003 01:00

As of 4.3.2:
$names = $contact->get_elements_by_tagname("name");
$name = $names[0];
$name_text_node = $name->first_child();
$name_text_node->set_content("Joe");
does not work.  Currently it looks like dom text nodes are implemented as "domtext", not "domelement" and therefore lack the set_content method.  As far as I've been able to tell, replacing is the best solution.


ericb

Actually, don't forget that the text is actually a child node of the given element. The domxml coders decided to append to the content in the case that this function is called on a node which has children (to avoid a crash), but you shouldn't normally call it that way.
For example, if your document had:
<contact>
<name>Eric</name>
</contact>
and you wanted to change the content of the name element and $contact is the contact node, then you would use:
$names = $contact->get_elements_by_tagname("name");
$name = $names[0];
$name_text_node = $name->first_child();
$name_text_node->set_content("Joe");
Hope this clarifies things. Removing nodes, and inserting new ones is not an efficient solution :)
Cheers,
Eric


kdan

/**
* Replace 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.
*/
function replace_content( &$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 );
}
}

$node->replace_node( $newnode );
}


Change Language


Follow Navioo On Twitter
DomAttribute->name
DomAttribute->set_value
DomAttribute->specified
DomAttribute->value
DomDocument->add_root
DomDocument->create_attribute
DomDocument->create_cdata_section
DomDocument->create_comment
DomDocument->create_element_ns
DomDocument->create_element
DomDocument->create_entity_reference
DomDocument->create_processing_instruction
DomDocument->create_text_node
DomDocument->doctype
DomDocument->document_element
DomDocument->dump_file
DomDocument->dump_mem
DomDocument->get_element_by_id
DomDocument->get_elements_by_tagname
DomDocument->html_dump_mem
DomDocument->xinclude
DomDocumentType->entities()
DomDocumentType->internal_subset()
DomDocumentType->name()
DomDocumentType->notations()
DomDocumentType->public_id()
DomDocumentType->system_id()
DomElement->get_attribute_node()
DomElement->get_attribute()
DomElement->get_elements_by_tagname()
DomElement->has_attribute()
DomElement->remove_attribute()
DomElement->set_attribute_node()
DomElement->set_attribute()
DomElement->tagname()
DomNode->add_namespace
DomNode->append_child
DomNode->append_sibling
DomNode->attributes
DomNode->child_nodes
DomNode->clone_node
DomNode->dump_node
DomNode->first_child
DomNode->get_content
DomNode->has_attributes
DomNode->has_child_nodes
DomNode->insert_before
DomNode->is_blank_node
DomNode->last_child
DomNode->next_sibling
DomNode->node_name
DomNode->node_type
DomNode->node_value
DomNode->owner_document
DomNode->parent_node
DomNode->prefix
DomNode->previous_sibling
DomNode->remove_child
DomNode->replace_child
DomNode->replace_node
DomNode->set_content
DomNode->set_name
DomNode->set_namespace
DomNode->unlink_node
DomProcessingInstruction->data
DomProcessingInstruction->target
DomXsltStylesheet->process()
DomXsltStylesheet->result_dump_file()
DomXsltStylesheet->result_dump_mem()
domxml_new_doc
domxml_open_file
domxml_open_mem
domxml_version
domxml_xmltree
domxml_xslt_stylesheet_doc
domxml_xslt_stylesheet_file
domxml_xslt_stylesheet
domxml_xslt_version
xpath_eval_expression
xpath_eval
xpath_new_context
xpath_register_ns_auto
xpath_register_ns
xptr_eval
xptr_new_context
eXTReMe Tracker