note 126755 added to ref.xml
| From: | biuro at dso dot biz dot pl | Date: | Tue, 11 Jan 2022 08:40:43 +0000 |
| Subject: | note 126755 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217480@lists.php.net to get a copy of this message | ||
A class proposition that can handle very large XML files
use
<?php
$domObj = new BigXMLToArray($fileData, true);
?>
<?php
class BigXMLToArray
{
public $array;
public function __construct($xml, $getRoot = false) {
$use_errors = libxml_use_internal_errors(true);
$xmlData = simplexml_load_string($xml);
if (false === $xmlData) {
$message = "Failed loading XML\r\n";
foreach(libxml_get_errors() as $error) {
$message = "\t" . $error->message . "\r\n";
}
// Do something about this error message , e.g.
// DsoMultiImporterLog::setError(['message' => $message, 'xml'
=> $xml], 'GLOBAL', __METHOD__);
} else {
$this->array = $this->xml2array($xmlData, $getRoot);
}
libxml_clear_errors();
libxml_use_internal_errors($use_errors);
}
private function getAttributesList($xmlObject, $out = [])
{
if (count($xmlObject) > 1) {
foreach ($xmlObject as $index => $node) {
if (is_object($node)) {
$out[$index] = $this->getAttributesList($xmlObject);
} else {
$out[$index] = $node;
}
}
} else {
foreach ($xmlObject as $index => $node) {
if (is_object($node)) {
$out = $this->getAttributesList($xmlObject);
} else {
$out = $node;
}
}
}
return $out;
}
private function xml2array($xmlObject, $isRoot = false, $out = [])
{
if ($isRoot) {
$out[$xmlObject->getName()] = $this->xml2array($xmlObject, false);
} elseif (is_array($xmlObject) || (is_object($xmlObject) && $xmlObject->count()
> 0)) {
foreach ($xmlObject->children() as $index => $node) {
$tmp = [];
if (is_object($node)) {
foreach($node->attributes() as $a => $b) {
if (is_object($b) ||is_array($b)) {
$tmp['attrib'][$a] = $this->getAttributesList((array)$b);
} else {
$tmp['attrib'][$a] = $b;
}
}
}
if (empty($tmp['attrib'])) {
$tmp = $this->xml2array($node, false);
} else {
$tmp['value'] = $this->xml2array($node, false);
}
if (!empty($out[$index])) {
if (is_string($out[$index])
|| is_numeric($out[$index])
|| (is_array($out[$index]) && $out[$index] !==
array_values($out[$index]))
) {
$out[$index] = [$out[$index]];
}
$out[$index][] = $tmp;
} else {
$out[$index] = $tmp;
}
}
}
else{
$out = json_decode($xmlObject);
if ($out === null and !empty((string) $xmlObject)) {
$out = (string) $xmlObject;
}
}
return $out;
}
}
?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 83.4.64.77)
----
Manual Page -- https://php.net/manual/en/ref.xml.php
Edit -- https://main.php.net/note/edit/126755
Del: integrated -- https://main.php.net/note/delete/126755/integrated
Del: useless -- https://main.php.net/note/delete/126755/useless
Del: bad code -- https://main.php.net/note/delete/126755/bad+code
Del: spam -- https://main.php.net/note/delete/126755/spam
Del: non-english -- https://main.php.net/note/delete/126755/non-english
Del: in docs -- https://main.php.net/note/delete/126755/in+docs
Del: other reasons-- https://main.php.net/note/delete/126755
Reject -- https://main.php.net/note/reject/126755
Search -- https://main.php.net/manage/user-notes.php