recursive method to create structure xml structure of object DATA

@param $data
@param Xml $xml
@param boolean $addCdata
@param Xml|DOMElement $parent
@return Xml
506675
protected function _arrayToXml($data, Xml $xml, $addCdata, $parent)
    {
        foreach ($data as $key => $value) {
            $key        = str_replace(' ', '_', $key);
            $attributes = [];
            $data       = '';

            if (is_object($value)) {
                try {
                    $data = Serializer::serialize($value);
                } catch (ExceptionInterface $exception) {
                    $this->_addException($exception);
                }

                $value = [
                    '@attributes' => ['serialized_object' => true],
                    $data
                ];
            }

            try {
                $isArray = is_array($value);

                if ($isArray && array_key_exists('@attributes', $value)) {
                    $attributes = $value['@attributes'];
                    unset ($value['@attributes']);
                }

                if ($isArray) {
                    $parent = $this->_convertArrayDataToXml(
                        $value,
                        $addCdata,
                        $xml,
                        $key,
                        $parent,
                        $attributes
                    );
                    continue;
                }

                $element = $this->_appendDataToNode($addCdata, $xml, $key, $value);
                $parent->appendChild($element);

            } catch (DOMException $exception) {
                $this->_addException($exception);
            }
        }

        return $xml;
    }
599630
public function build_xml( $data = null ) {

		if ( ! empty( $data ) ) {
			$this->set_data( $data );
		}

		if ( empty( $this->data ) || ! is_array( $this->data ) ) {
			return false;
		}

		$head  = '<' . '?' . 'xml version="1.0" encoding="utf-8" ' . '?' . '>' . "\r\n<items count=\"" . count( $this->data['items'] ) . "\">\r\n";
		$lines = '';

		foreach ( $this->data['items'] as $item ) {
			$line = "\t<item>\r\n";

			foreach ( $this->data['columns'] as $column => $label ) {
				if ( is_numeric( $column ) && ( ( is_object( $item ) && ! isset( $item->$column ) ) || ( is_array( $item ) && ! isset( $item[ $column ] ) ) ) ) {
					$column = $label;
				}

				$line .= $this->build_xml_level( $item, $column );
			}

			$line  .= "\t</item>\r\n";
			$lines .= $line;
		}

		$foot = '</items>';

		$this->built = $head . $lines . $foot;

		return $this->built;
	}
1205190
private function appendNode(\DOMNode $parentNode, $data, string $nodeName, string $key = null): bool
    {
        $node = $this->dom->createElement($nodeName);
        if (null !== $key) {
            $node->setAttribute('key', $key);
        }
        $appendNode = $this->selectNodeType($node, $data);
        // we may have decided not to append this node, either in error or if its $nodeName is not valid
        if ($appendNode) {
            $parentNode->appendChild($node);
        }

        return $appendNode;
    }
865688
public function to_xml($data = null, $structure = null, $basenode = null, $use_cdata = null)
	{
		if ($data == null)
		{
			$data = $this->_data;
		}

		is_null($basenode) and $basenode = \Config::get('format.xml.basenode', 'xml');
		is_null($use_cdata) and $use_cdata = \Config::get('format.xml.use_cdata', false);

		// turn off compatibility mode as simple xml throws a wobbly if you don't.
		if (ini_get('zend.ze1_compatibility_mode') == 1)
		{
			ini_set('zend.ze1_compatibility_mode', 0);
		}

		if ($structure == null)
		{
			$structure = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$basenode />");
		}

		// Force it to be something useful
		if ( ! is_array($data) and ! is_object($data))
		{
			$data = (array) $data;
		}

		foreach ($data as $key => $value)
		{
			// replace anything not alpha numeric
			$key = preg_replace('/[^a-z_\-0-9]/i', '', $key);

			// no numeric keys in our xml please!
			if (is_numeric($key))
			{
				// make string key...
				$key = (\Inflector::singularize($basenode) != $basenode) ? \Inflector::singularize($basenode) : 'item';
			}

			// if there is another array found recrusively call this function
			if (is_array($value) or is_object($value))
			{
				$node = $structure->addChild($key);

				// recursive call if value is not empty
				if( ! empty($value))
				{
					$this->to_xml($value, $node, $key, $use_cdata);
				}
			}

			else
			{
				// add single node.
				$encoded = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, "UTF-8");

				if ($use_cdata and ($encoded !== (string) $value))
				{
					$dom = dom_import_simplexml($structure->addChild($key));
					$owner = $dom->ownerDocument;
					$dom->appendChild($owner->createCDATASection($value));
				}
				else
				{
					$structure->addChild($key, $encoded);
				}
			}
		}

		// pass back as string. or simple xml object if you want!
		return $structure->asXML();
	}
1291355
protected function toXML(DOMElement $parent) : DOMElement
    {
        $e = $parent->ownerDocument->createElementNS(Constants::NS_MD, $this->elementName);
        $parent->appendChild($e);

        if ($this->ID !== null) {
            $e->setAttribute('ID', $this->ID);
        }

        if ($this->validUntil !== null) {
            $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->validUntil));
        }

        if ($this->cacheDuration !== null) {
            $e->setAttribute('cacheDuration', $this->cacheDuration);
        }

        $e->setAttribute('protocolSupportEnumeration', implode(' ', $this->protocolSupportEnumeration));

        if ($this->errorURL !== null) {
            $e->setAttribute('errorURL', $this->errorURL);
        }

        Extensions::addList($e, $this->Extensions);

        foreach ($this->KeyDescriptor as $kd) {
            $kd->toXML($e);
        }

        if ($this->Organization !== null) {
            $this->Organization->toXML($e);
        }

        foreach ($this->ContactPerson as $cp) {
            $cp->toXML($e);
        }

        return $e;
    }
186223
public function toXML(DOMElement $parent = null) : DOMElement
    {
        if (empty($this->entityID)) {
            throw new \Exception('Cannot convert EntityDescriptor to XML without an EntityID set.');
        }

        if ($parent === null) {
            $doc = DOMDocumentFactory::create();
            $e = $doc->createElementNS(Constants::NS_MD, 'md:EntityDescriptor');
            $doc->appendChild($e);
        } else {
            $e = $parent->ownerDocument->createElementNS(Constants::NS_MD, 'md:EntityDescriptor');
            $parent->appendChild($e);
        }

        $e->setAttribute('entityID', $this->entityID);

        if ($this->ID !== null) {
            $e->setAttribute('ID', $this->ID);
        }

        if ($this->validUntil !== null) {
            $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->validUntil));
        }

        if ($this->cacheDuration !== null) {
            $e->setAttribute('cacheDuration', $this->cacheDuration);
        }

        Extensions::addList($e, $this->Extensions);

        foreach ($this->RoleDescriptor as $n) {
            $n->toXML($e);
        }

        if ($this->AffiliationDescriptor !== null) {
            $this->AffiliationDescriptor->toXML($e);
        }

        if ($this->Organization !== null) {
            $this->Organization->toXML($e);
        }

        foreach ($this->ContactPerson as $cp) {
            $cp->toXML($e);
        }

        foreach ($this->AdditionalMetadataLocation as $n) {
            $n->toXML($e);
        }

        /** @var \DOMElement $child */
        $child = $e->firstChild;
        $this->signElement($e, $child);

        return $e;
    }
672540
public static function makeXML($model, $parent, $addToParent = false)
    {
        if (is_array($model)) {
            foreach ($model as $name => $value) {
                if (!is_numeric($name)) {
                    $node = $parent->ownerDocument->createElement($name);
                    $parent->appendChild($node);
                    self::makeXml($value, $node, true);
                } else {
                    $node = $parent;
                    self::makeXml($value, $node);
                }
            }
        } elseif (is_object($model)) {
            if ($addToParent) {
                $node = $parent;
            } else {
                $node = $parent->ownerDocument->createElement(get_class($model));
                $parent->appendChild($node);
            }
            foreach ($model as $propertyName => $propertyValue) {
                $property = $parent->ownerDocument->createElement($propertyName);
                $node->appendChild($property);
                self::makeXml($propertyValue, $property);
            }
        } else {
            $parent->appendChild($parent->ownerDocument->createTextNode($model));
        }
    }
74986
function addData( $cdata ) {
		if( !isset( $this->data[$this->row] ) ) {
			$this->data[$this->row] = array();
		}

		if( !isset( $this->data[$this->row][$this->current_field] ) ) {
			$this->data[$this->row][$this->current_field] = '';
		}

		$this->data[$this->row][$this->current_field] .= $cdata;
	}
346885
public function makeChild(array $attributes=[], Node $parent)
    {
        $child = $this->make($attributes);

        $child[$this->getParentIdName()] = $parent->getId();
        $child[$this->getRootIdName()] = $parent[$this->getRootIdName()];

        $child->setParent($parent);

        return $child;
    }
1137692
public function writeElement(
            string $elementName,
            array $attributes = [],
            string $cdata     = null
    ): XmlStreamWriter {
        return $this->append(
                function(\DOMNode $parent) use ($elementName, $attributes, $cdata)
                {
                    $element = $this->doc->createElement($elementName);
                    foreach ($attributes as $name => $value) {
                        $element->setAttribute($name, $this->encode($value));
                    }

                    if (null !== $cdata) {
                        $element->appendChild($this->doc->createTextNode($this->encode($cdata)));
                    }

                    $parent->appendChild($element);
                },
                'element: "' . $elementName . '"'
        );
    }
580981
public function toXML($data)
    {
        $root = isset($data['root']) ? $data['root'] : $this->module;
        $no = 1;
        $xml = '<'.$root.'>';
        if (isset($data['options'])) {
            $xml .= '<row no="'.$no.'">';
            foreach ($data['options'] as $key => $value) {
                $xml .= '<option val="'.$key.'">'.$value.'</option>';
            }
            $xml .= '</row>';
            ++$no;
        }
        foreach ($data['records'] as $row) {
            $xml .= '<row no="'.$no.'">';
            foreach ($row as $key => $value) {
                if (is_array($value)) {
                    $xml .= '<FL val="'.str_replace('&', 'and', $key).'">';
                    foreach ($value as $k => $v) {
                        list($tag, $attribute) = explode(' ', $k);
                        $xml .= '<'.$tag.' no="'.$attribute.'">';
                        foreach ($v as $kk => $vv) {
                            $xml .= '<FL val="'.str_replace('&', 'and', $kk).'"><![CDATA['.$vv.']]></FL>';
                        }
                        $xml .= '</'.$tag.'>';
                    }
                    $xml .= '</FL>';
                } else {
                    $xml .= '<FL val="'.str_replace('&', 'and', $key).'"><![CDATA['.$value.']]></FL>';
                }
            }
            $xml .= '</row>';
            ++$no;
        }
        $xml .= '</'.$root.'>';

        return $xml;
    }
996354
private function arrayToXml(array &$data, SimpleXMLElement $xmlData)
    {
        foreach ($data as $key => $value) {
            if (\is_array($value)) {
                if (\is_numeric($key)) {
                    $key = 'sequential-item';
                }
                $subnode = $xmlData->addChild($key);

                $this->arrayToXml($value, $subnode);
            } else {
                $subnode = $xmlData->addChild("$key", "$value");

                $type = \gettype($value);
                if ('array' !== $type) {
                    $subnode->addAttribute('type', $type);
                }
            }
        }
    }
213524
public function toXml($data = null, $structure = null, $basenode = null, $usecdata = null)
	{
		if ($data === null)
		{
			$data = $this->data;
		}

		if ($basenode === null)
		{
			$basenode = Arr::get($this->config, 'xml.basenode', 'xml');
		}

		if ($usecdata === null)
		{
			$usecdata = Arr::get($this->config, 'xml.usecdata', false);
		}

		if ($structure === null)
		{
			$structure = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$basenode />");
		}

		// Force it to be something useful
		if ( ! is_array($data) and ! is_object($data))
		{
			$data = (array) $data;
		}

		foreach ($data as $key => $value)
		{
			// replace anything not alpha numeric
			$key = preg_replace('/[^a-z_\-0-9]/i', '', $key);

			// no numeric keys in our xml please!
			if (is_numeric($key))
			{
				// make string key...
				$key = ($this->inflector->singularize($basenode) != $basenode) ? $this->inflector->singularize($basenode) : 'item';
			}

			// if there is another array found recrusively call this function
			if (is_array($value) or is_object($value))
			{
				$node = $structure->addChild($key);

				// recursive call if value is not empty
				if( ! empty($value))
				{
					$this->toXml($value, $node, $key, $usecdata);
				}
			}

			else
			{
				// add single node.
				$encoded = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, 'UTF-8');

				if ($usecdata and ($encoded !== (string) $value))
				{
					$dom = dom_import_simplexml($structure->addChild($key));
					$owner = $dom->ownerDocument;
					$dom->appendChild($owner->createCDATASection($value));
				}
				else
				{
					$structure->addChild($key, $encoded);
				}
			}
		}

		// pass back as string. or simple xml object if you want!
		return $structure->asXML();
	}
917938
private function createNodeFromArray(array $data = []): Node
    {
        if (null === $this->serializer) {
            $this->serializer = new PhpArraySerializer();
        }

        /** @var Node $node */
        $node = $this->serializer->deserialize($data);
        return $node;
    }
905117
protected static function buildTree(&$data, $rootID = 0)
    {
        $tree = [];

        foreach ($data as $id => $node) {
            if ($node->parentId == $rootID) {
                unset($data[$id]);
                $node->children = self::buildTree($data, $node->id);
                $tree[] = $node;
            }
        }

        return $tree;
    }
1070869
public function xml($data)
    {
        if ($data instanceof SimpleXMLElement) {
            $data = $data->saveXML();
        }
        $dom = new DOMDocument();
        $dom->loadXml($data);
        $dom->formatOutput = true;
        return $dom->saveXML();
    }
636314
protected static function arrayToXML($data, &$xml = null){

            if($xml === null)
                $xml = new \SimpleXMLElement("<?xml version=\"1.0\"?><nondb></nondb>");
            foreach($data as $k=>$v){
                if(is_array($v)){
                    $sub = $xml->addChild("$k");
                    self::arrayToXML($v, $sub);
                }else{
                    $xml->addChild("$k", htmlspecialchars("$v"));
                }
            }

            return $xml->asXML();

        }