Skip to content

Instantly share code, notes, and snippets.

@kernusr
Created March 17, 2021 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kernusr/2981c1b1c44046e290beb90b84cea02c to your computer and use it in GitHub Desktop.
Save kernusr/2981c1b1c44046e290beb90b84cea02c to your computer and use it in GitHub Desktop.
Вставить один simplexmlelement в другой
<?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