Created
March 17, 2021 10:10
-
-
Save kernusr/2981c1b1c44046e290beb90b84cea02c to your computer and use it in GitHub Desktop.
Вставить один simplexmlelement в другой
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SimpleXMLElementEx extends SimpleXMLElement{ | |
public function appendXML(\SimpleXMLElement $data): void | |
{ | |
$name = $data->getName(); | |
$nameSpaces = $data->getNamespaces(false); | |
$qualifiedName = $name; | |
$value = null; | |
$namespace = ""; | |
if (!empty($nameSpaces)) | |
{ | |
$prefix = array_key_first($nameSpaces); | |
if (is_string($prefix)) | |
{ | |
$qualifiedName = $prefix . ':' . $name; | |
$namespace = $nameSpaces[$prefix]; | |
} | |
else | |
{ | |
$namespace = $nameSpaces[0]; | |
} | |
} | |
if (strlen(trim((string) $data)) == 0) | |
{ | |
$xml = $this->addChild($qualifiedName, null, $namespace); | |
foreach ($data->children($namespace) as $child) | |
{ | |
$xml->appendXML($child); | |
} | |
} | |
else | |
{ | |
$xml = $this->addChild($qualifiedName, (string) $data, $namespace); | |
} | |
if (!empty($nameSpaces)) | |
{ | |
foreach ($nameSpaces as $p => $ns) | |
{ | |
foreach ($data->attributes($ns) as $n => $v) | |
{ | |
if (is_string($p)) | |
{ | |
$xml->addAttribute($p . ':' . $n, $v, $ns); | |
} | |
else | |
{ | |
$xml->addAttribute($n, $v, $ns); | |
} | |
} | |
} | |
} | |
else | |
{ | |
foreach ($data->attributes() as $n => $v) | |
{ | |
$xml->addAttribute($n, $v); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment