Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Вставить один 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