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



PHP : Function Reference : DOM Functions : DOMNodelist->item()

DOMNodelist->item()

Retrieves a node specified by index ()

Example 541. Traversing all the entries of the table

<?php

$doc
= new DOMDocument;
$doc->load('book.xml');

$items = $doc->getElementsByTagName('entry');

for (
$i = 0; $i < $items->length; $i++) {
   echo
$items->item($i)->nodeValue . "\n";
}

?>

Alternatively, you can use foreach, which is a much more convenient way:

<?php

foreach ($items as $item) {
   echo
$item->nodeValue . "\n";
}

?>

The above example will output:

Title
Author
Language
ISBN
The Grapes of Wrath
John Steinbeck
en
0140186409
The Pearl
John Steinbeck
en
014017737X
Samarcande
Amine Maalouf
fr
2253051209

Code Examples / Notes » dom_domnodelist_item

james dot dunmore

tfg_allardyce at gmail dot com
I have had exactly this problem.
To rectify I've had to do this:
<?php
$old_element = $doc->getElementsByTagName('Element1')->item(0);
$new_element = $doc->createElement('NewElement1');
$old_element_childNodes = $old_element->childNodes;
$length = $old_element_childNodes->length;
for($i = 0; $i < $length; $i++)
{
$oldChildren_array[] = $old_element_childNodes->item($i);
}
foreach($oldChildren_array as $old_c)
{
$new_element->appendChild($old_c);
}
?>
Rather than this:
(which I will bug report)
<?php
$old_element = $doc->getElementsByTagName('Element1')->item(0):
$new_element = $doc->createElement('NewElement1');
foreach($old_element->childNode as $node)
{
    $new_element->appendChild($node);
}
?>
Using the latter, randomally removes the children!


oliver dot christen

NodeList are something annoying because you can't output the content with a simple print_r, so I did a little function that add all the node to a new empty DOMDocument and output it as a string.
Have fun.
<?php
public function domNodeList_to_string($DomNodeList) {
   $output = '';
   $doc = new DOMDocument;
   while ( $node = $DomNodeList->item($i) ) {
       // import node
       $domNode = $doc->importNode($node, true);
       // append node
       $doc->appendChild($domNode);
       $i++;
   }
   $output = $doc->saveXML();
   $output = print_r($output, 1);
   // I added this because xml output and ajax do not like each others
   $output = htmlspecialchars($output);
   return $output;
}
?>


hayley watson

Keep in mind that DOMNodelists are "live" - changes to the document or node that the DOMNodelist was derived from will be reflected in the DOMNodelist. In other words, a list of a parent node's children will change if you change the parent's children!

tfg_allardyce

Be careful when looping through a DOMNodeList and moving its nodes around, sometimes this will take that node off the DOMNodeList and sometimes it wont!
<?php
// let $nodes be node list and $parent be some other node
foreach($nodes as $node) {
  $parent->appendChild($node);
}
?>
In some cases the $node will be taken off the list and the next iteration of the loop will be corrupted, skipping every other node in the list! In other cases the node will remain in the list and everything will be fine.
Generally if you've created the node list using a getElementsByTagName call or an XPath query then the nodes will stay on the list. If the node list comes from another nodes' childNodes property those child nodes will be shifted off the list whenever you call appendChild.


geoffrey thubron

@ tfg_allardyce at gmail dot com
You could loop through the list backwards, that way, you are only ever taking off the last item from the list, and hence wont have disrupted the order.


Change Language


Follow Navioo On Twitter
DOMAttr->__construct()
DOMAttr->isId()
DOMCharacterData->appendData()
DOMCharacterData->deleteData()
DOMCharacterData->insertData()
DOMCharacterData->replaceData()
DOMCharacterData->substringData()
DOMComment->__construct()
DOMDocument->__construct()
DOMDocument->createAttribute()
DOMDocument->createAttributeNS()
DOMDocument->createCDATASection()
DOMDocument->createComment()
DOMDocument->createDocumentFragment()
DOMDocument->createElement()
DOMDocument->createElementNS()
DOMDocument->createEntityReference()
DOMDocument->createProcessingInstruction()
DOMDocument->createTextNode()
DOMDocument->getElementById()
DOMDocument->getElementsByTagName()
DOMDocument->getElementsByTagNameNS()
DOMDocument->importNode()
DOMDocument->load()
DOMDocument->loadHTML()
DOMDocument->loadHTMLFile()
DOMDocument->loadXML()
DOMDocument->normalizeDocument()
DOMDocument->registerNodeClass()
DOMDocument->relaxNGValidate()
DOMDocument->relaxNGValidateSource()
DOMDocument->save()
DOMDocument->saveHTML()
DOMDocument->saveHTMLFile()
DOMDocument->saveXML()
DOMDocument->schemaValidate()
DOMDocument->schemaValidateSource()
DOMDocument->validate()
DOMDocument->xinclude()
DOMDocumentFragment->appendXML()
DOMElement->__construct()
DOMElement->getAttribute()
DOMElement->getAttributeNode()
DOMElement->getAttributeNodeNS()
DOMElement->getAttributeNS()
DOMElement->getElementsByTagName()
DOMElement->getElementsByTagNameNS()
DOMElement->hasAttribute()
DOMElement->hasAttributeNS()
DOMElement->removeAttribute()
DOMElement->removeAttributeNode()
DOMElement->removeAttributeNS()
DOMElement->setAttribute()
DOMElement->setAttributeNode()
DOMElement->setAttributeNodeNS()
DOMElement->setAttributeNS()
DOMElement->setIdAttribute()
DOMElement->setIdAttributeNode()
DOMElement->setIdAttributeNS()
DOMEntityReference->__construct()
DOMImplementation->__construct()
DOMImplementation->createDocument()
DOMImplementation->createDocumentType()
DOMImplementation->hasFeature()
DOMNamedNodeMap->getNamedItem()
DOMNamedNodeMap->getNamedItemNS()
DOMNamedNodeMap->item()
DOMNode->appendChild()
DOMNode->cloneNode()
DOMNode->hasAttributes()
DOMNode->hasChildNodes()
DOMNode->insertBefore()
DOMNode->isDefaultNamespace()
DOMNode->isSameNode()
DOMNode->isSupported()
DOMNode->lookupNamespaceURI()
DOMNode->lookupPrefix()
DOMNode->normalize()
DOMNode->removeChild()
DOMNode->replaceChild()
DOMNodelist->item()
DOMProcessingInstruction->__construct()
DOMText->__construct()
DOMText->isWhitespaceInElementContent()
DOMText->splitText()
DOMXPath->__construct()
DOMXPath->evaluate()
DOMXPath->query()
DOMXPath->registerNamespace()
dom_import_simplexml
eXTReMe Tracker