diff --git a/lib/Cake/Test/Case/Utility/XmlTest.php b/lib/Cake/Test/Case/Utility/XmlTest.php index b28a4efc0..820202c17 100644 --- a/lib/Cake/Test/Case/Utility/XmlTest.php +++ b/lib/Cake/Test/Case/Utility/XmlTest.php @@ -187,12 +187,23 @@ class XmlTest extends CakeTestCase { * * @dataProvider invalidDataProvider * @expectedException XmlException - * return void + * @return void */ public function testBuildInvalidData($value) { Xml::build($value); } +/** + * Test that building SimpleXmlElement with invalid XML causes the right exception. + * + * @expectedException XmlException + * @return void + */ + public function testBuildInvalidDataSimpleXml() { + $input = ' 'simplexml')); + } + /** * test build with a single empty tag * diff --git a/lib/Cake/Utility/Xml.php b/lib/Cake/Utility/Xml.php index f446798a3..19ea1725e 100644 --- a/lib/Cake/Utility/Xml.php +++ b/lib/Cake/Utility/Xml.php @@ -131,16 +131,23 @@ class Xml { if ($hasDisable && !$options['loadEntities']) { libxml_disable_entity_loader(true); } - if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') { - $xml = new SimpleXMLElement($input, LIBXML_NOCDATA); - } else { - $xml = new DOMDocument(); - $xml->loadXML($input); + try { + if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') { + $xml = new SimpleXMLElement($input, LIBXML_NOCDATA); + } else { + $xml = new DOMDocument(); + $xml->loadXML($input); + } + } catch (Exception $e) { + $xml = null; } if ($hasDisable && !$options['loadEntities']) { libxml_disable_entity_loader(false); } libxml_use_internal_errors($internalErrors); + if ($xml === null) { + throw new XmlException(__d('cake_dev', 'Xml cannot be read.')); + } return $xml; }