Doc #63679 [Opn->Csd]: "Dealing with XML errors" documentation example is wrong
| From: | kalle@php.net | Date: | Wed, 05 Dec 2012 09:11:24 +0000 |
| Subject: | Doc #63679 [Opn->Csd]: "Dealing with XML errors" documentation example is wrong | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-9261@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=63679&edit=1
ID: 63679
Updated by: kalle@php.net
Reported by: toby at caboteria dot org
Summary: "Dealing with XML errors" documentation example is
wrong
-Status: Open
+Status: Closed
Type: Documentation Problem
Package: Documentation problem
Operating System: n/a
PHP Version: Irrelevant
-Assigned To:
+Assigned To: kalle
Block user comment: N
Private report: N
New Comment:
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation better.
Previous Comments:
------------------------------------------------------------------------
[2012-12-05 09:11:19] kalle@php.net
Automatic comment from SVN on behalf of kalle
Revision: http://svn.php.net/viewvc/?view=revision&revision=328672
Log: Fixed #63679 ("Dealing with XML errors" documentation example is wrong)
------------------------------------------------------------------------
[2012-12-04 04:46:48] toby at caboteria dot org
I agree that there's no bug in php itself, it's the code in the example that's buggy.
I planned to leave a note, but the instructions indicated that I should file a bug report for
documentation bugs.
------------------------------------------------------------------------
[2012-12-04 00:32:56] mail+php at requinix dot net
Not a bug. As that very same note indicates it's a false negative: there was no
error. You just get an object that looks empty because you're doing a loose
comparison and PHP defines an "empty" object as false. The SimpleXMLElement looks
empty because the object is defined as containing all child nodes within a given
namespace; in the first example there are no child nodes at all, and in the second
example there are no child nodes in the default ("") namespace.
That note and the one after both give you ways to work around the loose-typing
issue: compare ===false or do a !($obj instanceof SimpleXMLElement).
------------------------------------------------------------------------
[2012-12-03 22:26:03] toby at caboteria dot org
Description:
------------
There are at least two cases where the example code on the "Dealing with XML errors"
page[0] won't work correctly. The first is mentioned in one of the notes[1]: a document with
an empty root node will show up as an error, even if it parses correctly. The second is if the
nodes just below the document node are in a different namespace.
I recommend using
if (libxml_get_errors()) {
instead of the existing code.
[0] http://php.net/manual/en/simplexml.examples-errors.php
[1] http://php.net/manual/en/simplexml.examples-errors.php#96416
Test script:
---------------
First case:
<?php
libxml_use_internal_errors(true);
$sxe = simplexml_load_string("<?xml version='1.0'?><not_broken/>");
if (!$sxe) {
echo "Failed loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
}
?>
Second case:
<?php
libxml_use_internal_errors(true);
$sxe = simplexml_load_string("<?xml version='1.0'?><not_broken
xmlns:m='urn:m'><m:foo></m:foo></not_broken>");
if (!$sxe) {
echo "Failed loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
}
?>
Expected result:
----------------
Empty page.
Actual result:
--------------
Failed loading XML
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63679&edit=1