Add support for the parseHuge option.

Sometimes people need to load huge XML files. Add an option to enable
people to enable this flag.

Refs #10031
This commit is contained in:
mark_story 2017-01-13 21:26:54 -05:00
parent d40b39f18b
commit 273a8a2d7d
2 changed files with 21 additions and 2 deletions

View file

@ -167,6 +167,18 @@ class XmlTest extends CakeTestCase {
$this->assertNotRegExp('/encoding/', $obj->saveXML()); $this->assertNotRegExp('/encoding/', $obj->saveXML());
} }
/**
* test build() method with huge option
*
* @return void
*/
public function testBuildHuge() {
$xml = '<tag>value</tag>';
$obj = Xml::build($xml, array('parseHuge' => true));
$this->assertEquals('tag', $obj->getName());
$this->assertEquals('value', (string)$obj);
}
/** /**
* Test that the readFile option disables local file parsing. * Test that the readFile option disables local file parsing.
* *

View file

@ -80,7 +80,9 @@ class Xml {
* - `readFile` Set to false to disable file reading. This is important to disable when * - `readFile` Set to false to disable file reading. This is important to disable when
* putting user data into Xml::build(). If enabled local & remote files will be read if they exist. * putting user data into Xml::build(). If enabled local & remote files will be read if they exist.
* Defaults to true for backwards compatibility reasons. * Defaults to true for backwards compatibility reasons.
* - If using array as input, you can pass `options` from Xml::fromArray. * - `parseHuge` Enable the `LIBXML_PARSEHUGE`
*
* If using array as input, you can pass `options` from Xml::fromArray.
* *
* @param string|array $input XML string, a path to a file, a URL or an array * @param string|array $input XML string, a path to a file, a URL or an array
* @param array $options The options to use * @param array $options The options to use
@ -94,7 +96,8 @@ class Xml {
$defaults = array( $defaults = array(
'return' => 'simplexml', 'return' => 'simplexml',
'loadEntities' => false, 'loadEntities' => false,
'readFile' => true 'readFile' => true,
'parseHuge' => true
); );
$options += $defaults; $options += $defaults;
@ -135,6 +138,10 @@ class Xml {
if ($hasDisable && !$options['loadEntities']) { if ($hasDisable && !$options['loadEntities']) {
libxml_disable_entity_loader(true); libxml_disable_entity_loader(true);
} }
$flags = LIBXML_NOCDATA;
if (!empty($options['parseHuge'])) {
$flags |= LIBXML_PARSEHUGE;
}
try { try {
if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') { if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
$xml = new SimpleXMLElement($input, LIBXML_NOCDATA); $xml = new SimpleXMLElement($input, LIBXML_NOCDATA);