Convert an Array to XML.

@param string $node_name - name of the root node to be converted
@param array  $data      - aray to be converterd

@return DomDocument
766913
public function &createXML($node_name, $data = [])
    {
        $xml = $this->getXMLRoot();
        $xml->appendChild($this->convert($node_name, $data));
        $this->xml = null;    // clear the xml node in the class for 2nd time use.
        return $xml;
    }
1125067
public static function toXml($data): string
    {
        if (!is_array($data) || count($data) <= 0) {
            throw new InvalidArgumentException('Convert To Xml Error! Invalid Array!');
        }

        $xml = '<xml>';
        foreach ($data as $key => $val) {
            $xml .= is_numeric($val) ? '<'.$key.'>'.$val.'</'.$key.'>' :
                                       '<'.$key.'><![CDATA['.$val.']]></'.$key.'>';
        }
        $xml .= '</xml>';

        return $xml;
    }
1328088
public function fieldElement(string $node_name, string $fieldname_attribute_name = ''): self
    {
        $clone = clone $this;
        $clone->field_name = $this->filterElementName($node_name);
        $clone->column_attr = $this->filterAttributeName($fieldname_attribute_name);

        return $clone;
    }
854170
public static function recursiveData2XML($data, string $item = 'item', string $id = 'id'): string {
		$xml = $attr = '';
		if (is_array($data)) {
			foreach ($data as $key => $val) {
				if (is_numeric($key)) {
					$id && $attr = " {$id}=\"{$key}\"";
					$key = $item;
				}
				$xml .= "<{$key}{$attr}>";
				$xml .= (is_array($val) || is_object($val)) ? static::recursiveData2XML($val, $item, $id) : $val;
				$xml .= "</{$key}>";
			}
		}
		elseif (is_string($data))
			return $data;
		else throw new EncodeXMLException;
		return $xml;
	}
53775
static function createElementNodeFromArray( $name, $array )
    {
        $dom = new DOMDocument( '1.0', 'utf-8' );
        $node = $dom->createElement( $name );
        $dom->appendChild( $node );

        foreach ( $array as $arrayKey => $value )
        {
            if ( is_array( $value ) and
                 count( $valueKeys = array_keys( $value ) ) > 0 )
            {
                if ( is_int( $valueKeys[0] ) )
                {
                    foreach( $value as $child )
                    {
                        unset( $childNode );
                        unset( $importedChildNode );
                        $childNode = eZContentObjectPackageHandler::createElementNodeFromArray( $arrayKey, $child );
                        $importedChildNode = $dom->importNode( $childNode, true );
                        $node->appendChild( $importedChildNode );
                    }
                }
                else
                {
                        unset( $valueNode );
                        unset( $importedValueNode );
                        $valueNode = eZContentObjectPackageHandler::createElementNodeFromArray( $arrayKey, $value );
                        $importedValueNode = $dom->importNode( $valueNode, true );
                        $node->appendChild( $importedValueNode );
                }
            }
            else
            {
                $node->setAttribute( $arrayKey, $value );
            }
        }
        return $node;
    }
893704
public static function create( $name, $content = NULL, $attributes = array(), $data = array() )
	{
		if( !strlen( $name	= trim( $name ) ) )
			throw new InvalidArgumentException( 'Missing tag name' );
		$name		= strtolower( $name );
		try{
			$attributes	= self::renderAttributes( $attributes );
			$data		= self::renderData( $data );
		}
		catch( InvalidArgumentException $e ) {
			if( version_compare( PHP_VERSION, '5.3.0', '>=' ) )
				throw new RuntimeException( 'Invalid attributes', NULL, $e );						//  throw exception and transport inner exception
			throw new RuntimeException( 'Invalid attributes', NULL );								//  throw exception
		}
		if( $content === NULL || $content === FALSE )												//  no node content defined, not even an empty string
			if( !in_array( $name, self::$shortTagExcludes ) )										//  node name is allowed to be a short tag
				return "<".$name.$attributes.$data."/>";											//  build and return short tag
		if( is_array( $content ) )																	//  content is an array, may be nested
			$content	= self::flattenArray( $content, '' );
		if( is_numeric( $content ) )
			$content	= (string) $content;
		if( is_object( $content ) ){
			if( !method_exists( $content, '__toString' ) ){											//  content is not a renderable object
				$message	= 'Object of class "'.get_class( $content ).'" cannot be rendered';		//  prepare message about not renderable object
				throw new InvalidArgumentException( $message ); 									//  break with error message
			}
			$content	= (string) $content;														//  render object to string
		}
		if( !is_null( $content ) && !is_string( $content ) ){										//  content is neither NULL nor string so far
			$message	= 'Content type "'.gettype( $content ).'" is not supported';				//  prepare message about wrong content data type
			throw new InvalidArgumentException( $message ); 										//  break with error message
		}
		return "<".$name.$attributes.$data.">".$content."</".$name.">";								//  build and return full tag
	}
444854
private function &convert($node_name, $arr = [])
    {
        $xml = $this->getXMLRoot();
        $node = $xml->createElement($node_name);
        if (is_array($arr)) {
            // get the attributes first.;
            if (isset($arr['@attributes'])) {
                foreach ($arr['@attributes'] as $key => $value) {
                    if (!$this->isValidTagName($key)) {
                        throw new Exception('[Array2XML] Illegal character in attribute name.'
                            . 'attribute: ' . $key . ' in node: ' . $node_name);
                    }
                    $node->setAttribute($key, $this->boolString($value));
                }
                unset($arr['@attributes']);
            }
            // check if it has a value stored in @value,
            // if yes store the value and return else check if its directly stored as string
            if (isset($arr['@value'])) {
                $node->appendChild($xml->createTextNode($this->boolString($arr['@value'])));
                unset($arr['@value']);

                return $node;
            } else {
                if (isset($arr['@cdata'])) {
                    $node->appendChild($xml->createCDATASection($this->boolString($arr['@cdata'])));
                    unset($arr['@cdata']);

                    return $node;
                }
            }
        }
        //create subnodes using recursion
        if (is_array($arr)) {
            // recurse to get the node for that key
            foreach ($arr as $key => $value) {
                if (!$this->isValidTagName($key)) {
                    throw new Exception('[Array2XML] Illegal character in tag name.'
                        . 'tag: ' . $key . ' in node: ' . $node_name);
                }
                if (is_array($value) && is_numeric(key($value))) {
                    // MORE THAN ONE NODE OF ITS KIND
                    // if the new array is numeric index, means it is array of nodes of the same kind
                    // it should follow the parent key name
                    foreach ($value as $k => $v) {
                        $node->appendChild($this->convert($key, $v));
                    }
                } else {
                    // ONLY ONE NODE OF ITS KIND
                    $node->appendChild($this->convert($key, $value));
                }
                unset($arr[$key]); //remove the key from the array once done.
            }
        }
        // after we are done with all the keys in the array (if it is one)
        // we check if it has any text value, if yes, append it.
        if (!is_array($arr) && $arr !== '') {
            $node->appendChild($xml->createTextNode($this->boolString($arr)));
        }

        return $node;
    }
1164539
public function createNode($nodeId, $data = array())
    {

        $node = new Graph\Node($nodeId, $data);
        $node->setGraph($this);
        $this->addNode($node);

        return $node;
    }
1194451
private function convertNodeToXML($data, $nodeName, $depth)
    {
        if (is_scalar($data))
        {
            //Treat it as a scalar, just add the node to the output
            $this->output .= str_repeat(static::TAB_CHARACTER, $depth);
            $this->output .= '<' . $nodeName . '>' . $data . '</' . $nodeName . '>' . PHP_EOL;

            return;
        }

        //Handle objects as arrays and arrays as arrays
        if (is_object($data))
        {
            //Sorry for the typecast
            $data = (array)$data;

            //We can assume an object is an associative array now
            $isAssociative = true;
        }
        else
        {
            //This is considered the "Fastest" way to figure out if an array is associative or not
            $isAssociative = ($data !== array_values($data));
        }


        if ($isAssociative)
        {
            //Associative array, treat each node as an element of the parent, indent each one
            $this->output .= str_repeat(static::TAB_CHARACTER, $depth);
            $this->output .= '<' . $nodeName . '>' . PHP_EOL;

            foreach ($data as $key => $value)
            {
                $this->convertNodeToXML($value, $key, $depth + 1);
            }

            $this->output .= str_repeat(static::TAB_CHARACTER, $depth);
            $this->output .= '</' . $nodeName . '>' . PHP_EOL;
        }
        else
        {
            //Integer indexed array (we hope), treat each node as a clone of the parent with no extra indent
            foreach ($data as $key => $value)
            {
                $this->convertNodeToXML($value, $nodeName, $depth);
            }
        }
    }
573025
protected static function data2Xml(
        array $data,
        string $item = 'item',
        string $id = 'id'
    ): string {
        $xml = $attr = '';
        foreach ($data as $key => $val) {
            if (is_numeric($key)) {
                $id && $attr = " {$id}=\"{$key}\"";
                $key = $item;
            }
            $xml .= "<{$key}{$attr}>";
            if ((is_array($val) || is_object($val))) {
                $xml .= static::data2Xml((array)$val, $item, $id);
            } else {
                $xml .= is_numeric($val) ? $val : static::cdata($val);
            }
            $xml .= "</{$key}>";
        }
        return $xml;
    }
935926
public static function fromArray( $data, $rootName = null, $nodeName = null, $addHeader = true )
    {
        $_xml = true === $addHeader ? '<?xml version="1.0" encoding="UTF-8" ?>' : null;

        if ( null !== $rootName )
        {
            $_xml .= Markup::openTag( $rootName );
        }

        $_string = null;

        if ( $data instanceof \Traversable )
        {
            foreach ( $data as $_key => $_value )
            {
                if ( is_numeric( $_key ) )
                {
                    $_key = $nodeName ?: 'node';
                }

                $_string .= Markup::tag( $_key, array(), static::fromArray( $_value, $nodeName ) );
            }
        }
        else
        {
            $_string = htmlspecialchars( $data, ENT_QUOTES );
        }

        //	Add the converted XML
        $_xml .= $_string;

        if ( null !== $rootName )
        {
            $_xml .= Markup::closeTag( $rootName );
        }

        return $_xml;
    }
1110238
protected static function convertXmlToArray(\SimpleXMLElement $data)
    {
        $result = [];
        foreach ((array) $data as $index => $node) {
            $result[$index] = (\is_object($node)) ? static::convertXmlToArray($node) : trim((string) $node);
        }

        return $result;
    }
1315789
public static function convertFromXMLString($root_element, $xml)
    {
        $doc = new DOMDocument('1.0', 'utf-8');
        $doc->loadXML($xml);

        return self::convertFromDOMDocument($root_element, $doc);
    }
988464
private function parseSetResultNodes(DOMElement $child, $name, array &$result = null)
    {
        if (!(is_array($result[$name]) && $this->valueIsList($result[$name]))) {
            return false;
        }

        $value = static::getPhpValue($child, null, $this);

        if (is_array($value) && $this->valueIsList($value)) {
            return $result[$name] = array_merge($result[$name], $value);
        }

        return $result[$name][] = $value;
    }
1353415
function to_xml(){
		if ($this->skip) return false;
		
		$data = array(
			'id' => $this->get_id()
		);
		for ($i=0; $i<sizeof($this->config->text); $i++){
			$extra = $this->config->text[$i]["name"];
			$data[$extra]=$this->data[$extra];
			if (is_null($data[$extra]))
			    $data[$extra] = "";
		}

		if ($this->userdata !== false)
			foreach ($this->userdata as $key => $value){
				if ($value === null)
					$data[$key]="";
				$data[$key]=$value;
			}

		return $data;
	}
411698
public static function toXML($data, $rootNodeName = 'entity', &$xml=null )
    {

        // turn off compatibility mode
        if ( ini_get('zend.ze1_compatibility_mode') == 1 ) ini_set ( 'zend.ze1_compatibility_mode', 0 );
        if ( is_null( $xml ) ) //$xml = simplexml_load_string( "" );
            $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");

        // loop through the data passed in.
        foreach( $data as $key => $value ) {
            $numeric = false;
            // no numeric keys in our xml please!
            if ( is_numeric( $key ) ) {
                $numeric = 1;
                $key = $rootNodeName;
            }
            // delete any char not allowed in XML element names
            $key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);
            // if there is another array found recrusively call this function
            if ( is_array( $value ) ) {
                $node = self::isAssoc( $value ) || $numeric ? $xml->addChild( $key ) : $xml;
                // recrusive call.
                if ($numeric) $key = 'anon';
                self::toXml( $value, $key, $node );
            } else {
                // add single node.
                $value = Html::encode($value);
                $xml->addChild($key, $value);
            }
        }
        // pass back as XML
        return $xml->asXML();
    }
37701
public function xml_encode($data, $root='xml', $item='item', $attr='', $id='id', $encoding='utf-8') {
        if(is_array($attr)){
            $_attr = array();
            foreach ($attr as $key => $value) {
                $_attr[] = "{$key}=\"{$value}\"";
            }
            $attr = implode(' ', $_attr);
        }
        $attr   = trim($attr);
        $attr   = empty($attr) ? '' : " {$attr}";
        $xml   = "<{$root}{$attr}>";
        $xml   .= self::data_to_xml($data, $item, $id);
        $xml   .= "</{$root}>";
        return $xml;
    }