Skip to content

Instantly share code, notes, and snippets.

@einpraegsam
Created February 6, 2019 12:30
Show Gist options
  • Save einpraegsam/472a2916b77c9a8f3beee0a854b148ae to your computer and use it in GitHub Desktop.
Save einpraegsam/472a2916b77c9a8f3beee0a854b148ae to your computer and use it in GitHub Desktop.
Alternative GeneralUtility::xml2array() without 10MB restriction from libxml - for import and export . Quick and dirty hack, exactly like to the original one in TYPO3.
<?php
namespace TYPO3\CMS\Core\Utility;
use GuzzleHttp\Exception\RequestException;
use TYPO3\CMS\Core\Core\ApplicationContext;
use TYPO3\CMS\Core\Core\ClassLoadingInformation;
use TYPO3\CMS\Core\Crypto\Random;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Core\Service\OpcodeCacheService;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
class GeneralUtility
{
public static function xml2array2($string, $NSprefix = '', $reportDocTag = false): array
{
$dom = new \DOMDocument();
$dom->loadXML($string, LIBXML_PARSEHUGE);
$xml = simplexml_load_string($dom->saveXML());
$array = self::xmlElementToArray($xml);
if ($reportDocTag === true) {
$array['_DOCUMENT_TAG'] = 'T3RecordDocument';
}
return $array;
}
protected static function xmlElementToArray(\SimpleXMLElement $xml): array
{
$array = [];
foreach ($xml->children() as $key => $children) {
if (is_a($children, \SimpleXMLElement::class)) {
$attributes = $children->attributes();
if (isset($attributes['index'])) {
$attributeArray = json_decode(json_encode($attributes['index']), true);
$key = $attributeArray[0];
}
$value = json_decode(json_encode($children), true);
if (isset($value[0])) {
$children = $value[0];
} else {
$children = self::xmlElementToArray($children);
}
}
$array[$key] = $children;
}
return $array;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment