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 | 19 Comments »

19 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. Esport Says:
    February 16th, 2022 at 01:42

    … [Trackback]

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

  4. buy cz guns USA online Says:
    February 23rd, 2022 at 15:15

    … [Trackback]

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

  5. fullz shop Says:
    March 9th, 2022 at 06:03

    … [Trackback]

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

  6. cvv fullz 1000$ Says:
    April 22nd, 2022 at 01:40

    … [Trackback]

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

  7. pgslot99th Says:
    April 30th, 2022 at 20:33

    … [Trackback]

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

  8. Godrej Bettahalli Says:
    June 9th, 2022 at 10:50

    … [Trackback]

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

  9. https://krajowy.biz/ Says:
    June 9th, 2022 at 16:18

    … [Trackback]

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

  10. legal psychedelics for sale usa​ Says:
    June 21st, 2022 at 16:43

    … [Trackback]

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

  11. Online Medications Store Says:
    June 24th, 2022 at 14:35

    … [Trackback]

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

  12. ขายเฟสบุ๊คยิงแอด Says:
    July 2nd, 2022 at 19:21

    … [Trackback]

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

  13. สล็อตวอเลท ไม่มีขั้นต่ำ Says:
    July 6th, 2022 at 19:23

    … [Trackback]

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

  14. Watch Bhool Bhulaiyaa 2 Says:
    July 31st, 2022 at 15:25

    … [Trackback]

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

  15. sbobet Says:
    September 7th, 2022 at 01:29

    … [Trackback]

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

  16. cabo mexico real estate Says:
    September 16th, 2022 at 21:35

    … [Trackback]

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

  17. What does a high potency mushroom look like? Says:
    October 6th, 2022 at 08:12

    … [Trackback]

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

  18. best passive income Says:
    October 13th, 2022 at 14:09

    … [Trackback]

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

  19. aplicativo Says:
    October 19th, 2022 at 14:13

    … [Trackback]

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

Comments