php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #63679 "Dealing with XML errors" documentation example is wrong
Submitted: 2012-12-03 22:26 UTC Modified: 2012-12-05 13:57 UTC
From: toby at caboteria dot org Assigned: kalle (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: n/a
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
30 - 19 = ?
Subscribe to this entry?

 
 [2012-12-03 22:26 UTC] 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 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-04 00:32 UTC] 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-04 04:46 UTC] 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-05 09:11 UTC] kalle@php.net
Automatic comment from SVN on behalf of kalle
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=328672
Log: Fixed #63679 (&quot;Dealing with XML errors&quot; documentation example is wrong)
 [2012-12-05 09:11 UTC] kalle@php.net
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.


 [2012-12-05 09:11 UTC] kalle@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: kalle
 [2012-12-05 13:57 UTC] toby at caboteria dot org
Thanks for the speedy resolution!
 [2020-02-07 06:08 UTC] phpdocbot@php.net
Automatic comment on behalf of kalle
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=ef2c8a7a953336a12e3e37b449c0c9d7da39f2c9
Log: Fixed #63679 (&quot;Dealing with XML errors&quot; documentation example is wrong)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC