Bug #64104 [Opn->Nab]: Accessing childNodes of DOM element fails
Edit report at https://bugs.php.net/bug.php?id=64104&edit=1
ID: 64104
Updated by: johannes@php.net
Reported by: me at gowthams dot in
Summary: Accessing childNodes of DOM element fails
-Status: Open
+Status: Not a bug
Type: Bug
Package: DOM XML related
Operating System: All
PHP Version: 5.4.11
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
When enabling error reporting your script prints
Question 1<br />
Fatal error: Call to a member function item() on a non-object in - on line 22
When looking a bit close, i.e. doing
foreach ($lis as $li) {
var_dump($li->nodeName);
}
You get output like
string(2) "li"
string(5) "#text"
string(2) "li"
string(5) "#text"
which shows you there are text nodes after the <li> tags,
Previous Comments:
------------------------------------------------------------------------
[2013-01-30 10:15:42] me at gowthams dot in
Description:
------------
I found this error while trying to parse an ordered list. When I access the firstChild or the
lastChild of a DOM object that is in turn derived from another DOM object , the value is printed
perfectly.
However, when I try to access childDomObject->childNodes->item(0), the code fails.
Test script:
---------------
<?php
$html_data =
'<html><body>
<ol>
<li><strong>Question 1</strong> Answer1</li>
<li><strong>Question 2</strong> Answer2</li>
</ol>
</body></html>';
$doc = new DOMDocument();
$doc->loadHTML($html_data);
$xpath = new DOMXPath($doc);
$ols = $xpath->query('//ol');
$ol = $ols->item(0);
$lis = $ol->childNodes;
foreach ($lis as $li) {
//echo $li->firstChild->nodeValue."<br />";
//echo $li->lastChild->nodeValue."<br />";
echo $li->childNodes->item(0)->nodeValue."<br />";
}
Expected result:
----------------
Question 1 Answer1<br />
Question 2 Answer2<br />
Actual result:
--------------
Question 1<br />
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64104&edit=1
Thread (2 messages)