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



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

DomNode->child_nodes

Returns children of node ()
array DomNode->child_nodes ( )


Code Examples / Notes » domnode_child_nodes

jon.sprague

To answer hrosas)at(linuxmail)dot(org.
<node>
<child/>
</node>
<node> has 3 children.
1. whitespace between <node> and <child/>
2. <child/>
3. whitespace between <child/> and </node>
The whitespace is treated like text. To avoid this remove all whitespace.
<node><child/></node>
This can get hard to read, but correct.
But, you can tell some programs to ignore the whitespace, so it remains easy to read.
Jon Sprague


grillen

There were some comments about problems with whitespaces.
you can easily solve them by creating the dom-document as follows:
<?php
$xmlDoc = @domxml_open_mem(
$xmlString
, DOMXML_LOAD_PARSING + //0
DOMXML_LOAD_COMPLETE_ATTRS + //8
DOMXML_LOAD_SUBSTITUTE_ENTITIES + //4
DOMXML_LOAD_DONT_KEEP_BLANKS //16
, $error
);
?>
if you call child_nodes() on eg. the root-element whitespaces are ignored.


andrew

Sometimes unless you know exactly where elements are it can be time consuming to pick each thing up with get_element_by_tagname calls.
I used a quick little function that, given a structure such as:
<customer>
 <firstname>Joe</firstname>
 <surname>Bloggs</surname>
 <age>24</age>
 <sex>Male</sex>
 ...
</customer>
it will return an associative array with the tag names as keys and the content as values. During the load I strip whitespace as well so it isn't confused by \n's creating blank nodes
-- CODE --
$xmldoc = domxml_open_mem($xmlstring,
 DOMXML_LOAD_PARSING + //0
 DOMXML_LOAD_COMPLETE_ATTRS + //8
 DOMXML_LOAD_SUBSTITUTE_ENTITIES + //4
 DOMXML_LOAD_DONT_KEEP_BLANKS //16 ));
 // get the node root and then the children of it.
 $noderoot = $xmldoc->document_element();
 $childnodes = $noderoot->child_nodes();
 // build the array
 foreach ($childnodes as $value) {
     echo $value->tagname . " : " . $value->get_content() . "
";
     $nodeArray[$value->tagname] = $value->get_content();
 }
-- END CODE --
You can then return node array or start using it however you like in other functions just like a normal associative array then.
$nodeArray['firstname'] == "Joe"


zombie

Loop through children making sure that the children are not text nodes.
<pre>
$moduleDoc = domxml_open_file("main_module_defs.xml");
$moduleDefinitionXPath = xpath_new_context($moduleDoc);
$dConvsNodeSet = xpath_eval($moduleDefinitionXPath, "//modules/module[@name='search']/dConvs");
$children = $dConvsNodeSet->nodeset[0]-> child_nodes();
$dTotal = count($children);
for ($i=0;$i<$dTotal;$i++){
$curNode = $children[$i];
if($curNode->node_name() != "#text"){
echo "This are probably TextNodes..."; }
}
</pre>
--
http://artattack.to


j dot metzger

if no children present, method
returns
FALSE (bool)
you can check the return result like this:
$children = $domNode -> child_nodes();
if (is_bool($children))
  print "Sorry, no children present ...";


hrosas

I was using child_nodes and if I have a node like this.
<node>
   <child/>
</node>
the node <node> have two children: one the "    " spaces that appear as #text on php and <child/>. Is that normal? I think node has just one child <child/>


zombie

Here is a handy little function i started using at work and home quite a bit. It takes in an array of nodes, typically from DomNode->child_nodes() as well as a filter argument and returns an array of filtered out nodes.
If the $filter arg is a string, then the $boolean argument is used. If boolean is true, then only nodes named $filter will be returned. If boolean is false, then only nodes NOT named $filter will be returend.
Additionally, you can specify the filter as an assoc array in the form of $array[$node_name] = true|false and only nodes in the array with a true value are returend. Note that if it is an array, then $boolean is not used. I was planning on having the boolean act as a negation kinda like !in_array(), but it was more for completeness than need so i dropped it.
function get_filtered_children($children_array,$filter,$boolean = true){
$total_children = count($children_array);
for ($i = 0; $i < $total_children; $i++){
$cur_child = $children_array[$i];
$node_name = $cur_child->node_name();
if (is_array($filter)){ //array in the form of [node_name] = true|false
if ($filter[$node_name])
$filtered_children_array[] = $cur_child;
}
else if(is_string($filter)){ //string hopefully...
if ($boolean && $node_name == $filter)
$filtered_children_array[] = $cur_child;
elseif (!$boolean && $node_name != $filter)
$filtered_children_array[] = $cur_child;
}
else
user_error('filter is of type ' . gettype($filter) . '. Please only supply arrays or strings.', E_USER_ERROR);
}
return $filtered_children_array;
}
Blaine Garrett
Web Master of the Art Attack
http://artattack.to


arcana

A better way to check for Text Child nodes (PHP 4.3.0)
if ($childnode->node_type() == XML_TEXT_NODE)
{
echo "<br/>This item is a text node.";
}
else
{
echo "<br/>This item is not a text node.";
}
You may want to look at DomElement->is_blank_node() as well.


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