Compdigitec Labs

« | Home | »

DOMNodeList as an Array

By admin | August 31, 2008

When using getElementsByTagName, it returns a DOMNodeList which you can use with foreach, but you can’t use array indexes to access elements.

For example, you can use the following code to the contents of the first “test” tag:

<?php
$file
= 'z.xml';
$xmlfile = new DOMDocument();
@
$xmlfile->load($file);
$iplist = $xmlfile->getElementsByTagName('test');
$i = 0;
foreach(
$iplist as $v) {
if(
$i == 0) {
var_dump($v->nodeValue);
}
$i++;
}
?>

Use this as z.xml:
<?xml version="1.0" ?><test>Testing: hello, world</test>

So as you can see, the $iplist variable is acting like an array (via foreach behaviour). But try this code:

<?php
$file
= 'z.xml';
$xmlfile = new DOMDocument();
@
$xmlfile->load($file);
$iplist = $xmlfile->getElementsByTagName('test');
var_dump($iplist[0]->nodeValue);
?>

The error “Fatal error: Cannot use object of type DOMNodeList as array in /usr/dev/php5/error.php on line 6” should occur. To fix this, use $iplist->item(0) instead of $iplist[0], though you can still use DOMNodeList with foreach. The following code should work:

<?php
$file
= 'z.xml';
$xmlfile = new DOMDocument();
@
$xmlfile->load($file);
$iplist = $xmlfile->getElementsByTagName('test');
var_dump($iplist->item(0)->nodeValue);
?>

If you found this article helpful or useful, please help Compdigitec by spreading the word or by leaving a comment. Don’t forget to subscribe to Compdigitec Labs for more interesting articles!

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: PHP | 12 Comments »

12 Responses to “DOMNodeList as an Array”

  1. alboNicca Says:
    April 1st, 2012 at 20:10

    Thank u. That’s what I needed

  2. anon Says:
    August 10th, 2012 at 17:13

    Thank you very much.

  3. บริการรถพยาบาลเอกชน Says:
    July 2nd, 2026 at 09:20

    … [Trackback]

    […] Info on that Topic: compdigitec.com/labs/2008/08/31/domnodelist-as-an-array/ […]

  4. เสริมจมูกที่จีน Says:
    July 4th, 2026 at 04:34

    … [Trackback]

    […] Find More on on that Topic: compdigitec.com/labs/2008/08/31/domnodelist-as-an-array/ […]

  5. Hot to Burn Says:
    July 7th, 2026 at 23:45

    Hello my loved one! I want to say that this
    article is amazing, great written and include approximately all important infos.
    I would like to peer extra posts like this .

  6. รับวางระบบ Network Server กรุงเทพฯและปริมณฑล Says:
    July 14th, 2026 at 19:36

    … [Trackback]

    […] Read More Information here on that Topic: compdigitec.com/labs/2008/08/31/domnodelist-as-an-array/ […]

  7. grd plus Says:
    July 16th, 2026 at 06:46

    … [Trackback]

    […] Find More here to that Topic: compdigitec.com/labs/2008/08/31/domnodelist-as-an-array/ […]

  8. จดทะเบียนบริษัท Says:
    July 17th, 2026 at 22:34

    … [Trackback]

    […] Find More here on that Topic: compdigitec.com/labs/2008/08/31/domnodelist-as-an-array/ […]

  9. PEP Says:
    July 27th, 2026 at 03:28

    … [Trackback]

    […] Here you can find 2604 more Info to that Topic: compdigitec.com/labs/2008/08/31/domnodelist-as-an-array/ […]

  10. ABM888 แหล่งรวมสล็อต PG888 Says:
    August 7th, 2026 at 20:42

    … [Trackback]

    […] Information to that Topic: compdigitec.com/labs/2008/08/31/domnodelist-as-an-array/ […]

  11. ค่าน้ำแทงบอล Says:
    August 20th, 2026 at 20:08

    … [Trackback]

    […] Information to that Topic: compdigitec.com/labs/2008/08/31/domnodelist-as-an-array/ […]

  12. sidegra Says:
    August 28th, 2026 at 19:37

    … [Trackback]

    […] There you will find 78840 additional Information on that Topic: compdigitec.com/labs/2008/08/31/domnodelist-as-an-array/ […]

Comments