PHP: DOMNodelist->item() - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMProcessingInstruction->__construct() DOMNode->replaceChild()

Last updated: Sat, 27 Jan 2007

view this page in

# DOMNodelist->item()

(no version information, might be only in CVS)

DOMNodelist->item() -- Retrieves a node specified by index

## Описание

class DOMNodeList {
DOMNode item ( int index )
}

Retrieves a node specified by `index` within the DOMNodeList object.

> Подсказка: If you need to know the number of nodes in the collection, use the `length` property of the DOMNodeList object.

## Список параметров

`index`

  Index of the node into the collection.

## Возвращаемые значения

The node at the `index`th position in the DOMNodeList, or `NULL` if that is not a valid index.

## Примеры

Пример 1. 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"; } ?>`

Результат выполнения данного примера:

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

add a note User Contributed Notes
DOMNodelist->item()
oliver dot christen at camptocamp dot com
13-Feb-2007 07:27
`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;
}
?>`
tfg_allardyce at gmail dot com
10-Jan-2007 03:57
`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.`
add a note
DOMProcessingInstruction->__construct() DOMNode->replaceChild()

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST