php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #55184 DOMDocument::load() throws an exception instead of returning false on error
Submitted: 2011-07-11 22:14 UTC Modified: 2011-08-19 09:20 UTC
From: stuart at horuskol dot net Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: stuart at horuskol dot net
New email:
PHP Version: OS:

 

 [2011-07-11 22:14 UTC] stuart at horuskol dot net
Description:
------------
---
From manual page: http://www.php.net/domdocument.load%23Errors/Exceptions
---

The manual page reports that this method returns false on failure - however, testing this function has resulted in exceptions being thrown instead.

Can the manual be updated to reflect this?

Test script:
---------------
Example XML files:
http://horuskol.net/media/php/map-not-xml.xml
http://horuskol.net/media/php/map-malformed-xml.xml

<?php

$dom = new DOMDocument();
try {
  if (!$dom->load('not-a-file')) {
    throw new Exception('Error loading source file');
  }
} catch (Exception $e) {
  echo $e->getMessage();
}

try {
  if (!$dom->load('map-not-xml.xml')) {
    throw new Exception('Error loading source file');
  }
} catch (Exception $e) {
  echo $e->getMessage();
}

try {
  if (!$dom->load('map-malformed-xml.xml')) {
    throw new Exception('Error loading source file');
  }
} catch (Exception $e) {
  echo $e->getMessage();
}

Expected result:
----------------
Error loading source file
Error loading source file
Error loading source file

Actual result:
--------------
DOMDocument::load(): I/O warning : failed to load external entity "not-a-file"
DOMDocument::load(): Start tag expected, '<' not found in map-not-xml.xml, line: 1
DOMDocument::load(): Couldn't find end of Start Tag malformed line 2 in map-malformed-xml.xml, line: 2

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-08-17 13:26 UTC] bjori@php.net
-Status: Open +Status: Feedback
 [2011-08-17 13:26 UTC] bjori@php.net
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.php.net/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.


I'm confused. You are saying it throws exceptions..
But your example clearly shows it doesn't throw exceptions, just prints warnings 
and returns false..
 [2011-08-18 09:41 UTC] stuart at horuskol dot net
Hi,

If you look at my code you can see that I am catching the exceptions thrown by the call to DOMDocument::load and outputting the message in the exception. It is definitely not returning false.
 [2011-08-19 09:20 UTC] rquadling@php.net
-Status: Feedback +Status: Bogus
 [2011-08-19 09:20 UTC] rquadling@php.net
<?php
$s_None = 'http://horuskol.net/media/php/map-not-xml.xml';
$s_Bad = 'http://horuskol.net/media/php/map-malformed-xml.xml';
$dom = new DOMDocument();
var_dump($dom->load('not-a-file'));
var_dump($dom->load($s_None));
var_dump($dom->load($s_Bad));
?>

outputs ...


Warning: DOMDocument::load(): I/O warning : failed to load external entity 
"file:///C:/Program%20Files/ImageMagick/not-a-file" in - on line 5
bool(false)

Warning: DOMDocument::load(): Premature end of data in tag pre line 1 in 
http://horuskol.net/media/php/map-not-xml.xml, line: 6 in - on line 6
bool(false)

Warning: DOMDocument::load(): Premature end of data in tag pre line 1 in 
http://horuskol.net/media/php/map-malformed-xml.xml, line: 6 in - on line 7
bool(false)


As documented.

Also, your code outputs ...

Warning: DOMDocument::load(): I/O warning : failed to load external entity 
"file:///C:/Program%20Files/ImageMagick/not-a-file" in - on line 10
Error loading source file
Warning: DOMDocument::load(): I/O warning : failed to load external entity 
"file:///C:/Program%20Files/ImageMagick/map-not-xml.xml" in - on line 18
Error loading source file
Warning: DOMDocument::load(): I/O warning : failed to load external entity 
"file:///C:/Program%20Files/ImageMagick/map-malformed-xml.xml" in - on line 26
Error loading source file

Which contains both the warning and the thrown exception.

NFF.
 [2011-09-13 17:37 UTC] vaseem dot mca46 at gmail dot com
I used the below code and the file is in uploads folder.

$doc = new DOMDocument();
$doc->load("uploads/p25661072.xml");

I got this warning below. Please let me know how to solve this.

"Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "file:///C:/Program%20Files/Apache%20Software%20Foundation/Apache2.2/htdocs/ConugamSMS/includes/uploads/p25661072.xml" in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\ConugamSMS\includes\surveytaking.php on line 74"
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Mar 31 08:01:26 2020 UTC