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



PHP : Function Reference : DOM XML Functions : DomDocument->get_elements_by_tagname

DomDocument->get_elements_by_tagname

Returns array with nodes with given tagname in document or empty array, if not found ()
array DomDocument->get_elements_by_tagname ( string name )

Related Examples ( Source code ) » domdocument get elements by tagname
















Code Examples / Notes » domdocument get elements by tagname

salman

Yes it does return an object; the object is of type DomElement. Hopefully this code will help:
$element = new DomElement();
if(!$dom = domxml_open_file($xml_file)) {
  die("Error opening xml file");
}
$tables = $dom->get_elements_by_tagname("table");
foreach($tables as $table) {
   $element = $table;
   echo "Attribute name: ", $element->get_attribute("name");
}
- Salman
http://wwww.setcomputing.com/


jon

Warning to win32 users: It appears that use of the wildcard argument to this function (e.g.
<?php
 $alltags = $domdoc->get_elements_by_tagname("*");
?>
) to return a collection of all DomElements in a document, which worked in php4.3.0-4.3.3, is broken in php4.3.4 for Windows. Upgrading broke numerous scripts depending on this behaviour, which returned to normal as soon as I rolled back to 4.3.3-win32.
I had intended to offer a workaround using child_nodes(), but found problems with this also. Therefore my suggested workarounds are either (a) don't upgrade to 4.3.4 if you're running this extension on Windows and are depending on the wildcard behaviour or (b) rewrite your code so as to use actual tagnames
The wildcard usage is per the W3C DOM spec, so hopefully this will be fixed and available again in 4.3.5.


blah

To clarify some confusion:
This function returns an integer-indexed array of DomElement objects. The DomElement class extends the DomNode class, so all DomNode methods are available to this new object. Additional notes can be found under the "DomElement->get_elements_by_tagname" manual section. DomElement uses the DomNode method (unless DomElement overrides this method, but I don't think that occurs).
Also note, if there are no nodes matching your criteria, it returns an empty array, not false.


kinggeoffrey

The return looks like an integer indexed array of objects when i try it.
Versions
php:4.2.1 domxml:2.4.9
Code fragment:
$nodes = $doc->get_elements_by_tagname("user");
reset($nodes);
while (list($key, $value) = each($nodes)) {
echo "$key = " . $value->get_attribute('username') . "<br />\n";
}
Output fragment:
0 = a
1 = b
2 = c


scouture

Seems that this function return an object instead of an array. I've tried this :
if(!$dom = domxml_open_file("g://program files/apache group/apache/htdocs/intranetcw/data/config_one.xml"))
{
 echo "Error while parsing the document\n";
 exit;
}
//array DomDocument->get_elements_by_tagname ( string name)
$array_element = $dom->get_elements_by_tagname("IP_ORACLE_CONFIG");
if ($array_element)
{
echo count($array_element)." array count
";
for ($i=0;$i<count($array_element);$i++)
{
echo $array_element[$i]." array content
";
}
}
else
{
echo "you get nothing";
}
and the result was :
1 array count
Object array content
Styve


blah

Correction to my last note: DomElement (and DomDocument) have their own get_elements_by_tagname() method. It does not get it from DomNode.

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