convert a data tree to an XML tree with the name specified as the
prefix

@param mixed any PHP data type that is being converted to XML
@param string the name of the node that the data will be built onto
@retval DOMNode the newly created node
545647
public function convertToXml($data, $prefix)
    {
        $nodeName = $prefix;
        // figure out the prefix
        if (!self::isValidPrefix($prefix)) {
            throw new DomiException("invalid prefix '$prefix'");
        }
        
        // if the data needs a list node, change the name to use the list-suffix
        if (self::isListNode($data)) {
            $nodeName = $prefix . $this->listSuffix;
        }

        switch (self::getDataType($data)) {
            // if this array has attributes, do some additional work
            case self::DT_ATTR_ARRAY:
                // create the node, with the optionally specified value
                $node = $this->createElement(
                    $nodeName, 
                    isset($data['values']) ? $data['values'] : null
                );
                $data['attributes'] = 
                    isset($data['attributes']) ? 
                    $data['attributes'] : 
                    array();
                
                // set all of the attributes onto the node
                foreach ($data['attributes'] as $key=>$val) {
                    $node->setAttribute($key, $val);
                }

                // remove the attributes and value so they aren't repeated
                // as children of the element
                unset($data['attributes']);
                unset($data['values']);
            case self::DT_ARRAY:
                // in the case of DT_ATTR_ARRAY, the node is already created
                if (!isset($node)) {
                    $node = $this->createElement($nodeName);
                }

                // attach each child as a subnode
                foreach ($data as $k=>$d) {
                    // figure out the child prefix
                    $childPrefix = self::isValidPrefix($k) ? $k : $prefix;
                    // recurse and attach
                    $node->appendChild($this->convertToXml($d, $childPrefix));
                }
                break;
            
            // when converting DOMi or DOMDocuments, just get the root node
            case self::DT_DOMI:
                // no break
            case self::DT_DOMDOCUMENT:
                $data = $data->childNodes->item(0);
                // no break
            case self::DT_DOMNODE:
                // the node must be imported to be usable in this DOMDocument
                $domNode = $this->importNode($data, true);
                // only create a new node if the prefix and current root
                // node name aren't the same
                if ($prefix == $domNode->nodeName) {
                    $node = $domNode;
                } else {
                    $node = $this->createElement($prefix);
                    $node->appendChild($domNode);
                }
                break;
            
            case self::DT_OBJECT:
                $node = $this->convertToXml(
                    $this->convertObjectToArray($data), 
                    $prefix
                );
                break;
            
            case self::DT_BOOL:
                $data = $data ? 'TRUE' : 'FALSE';
                // no break
            default:
                $node = $this->createElement(
                    $nodeName, 
                    htmlspecialchars((string)$data)
                );
                break;
        }
        
        return $node;
    }
639869
public function buildXML()
    {
        $xml = new EncodingRequest();

        $request = $xml->setDomQuery($this->client->getConfig('userid'), $this->client->getConfig('userkey'), $this->getName());

        foreach ($this->getOperation()->getParams() as $name => $arg) {

            if ($this->get($name) === true) {
                $request->appendChild($xml->createElement($name));
            } else {
                if (! is_null($this->get($name)) && $this->get($name) !== false) {

                    if (! is_array($this->get($name))) {
                        // Non Array Type - String, Integer, etc ..

                        $request->appendChild($xml->createElement($name, $this->get($name)));
                    } else {

                        if ($name == "source") {
                            // Build "Flat" Array to XML

                            foreach ($this->get($name) as $key => $value) {
                                $request->appendChild($xml->createElement($name, $value));
                            }
                        } else {
                            // Build Array to XML

                            $arrayRoot = $request->appendChild($xml->createElement($name));

                            foreach ($this->get($name) as $key => $value) {

                                if (! is_array($value)) {
                                    $arrayRoot->appendChild($xml->createElement($key, $value));
                                } else {
                                    if ($key == "destination") {
                                        // Build "Flat" Array to XML in a sub Element
                                        foreach ($value as $subValue) {
                                            $arrayRoot->appendChild($xml->createElement($key, $subValue));
                                        }
                                    } else {
                                        $arraySubRoot = $arrayRoot->appendChild($xml->createElement($key));

                                        foreach ($value as $subKey => $subValue) {
                                            $arraySubRoot->appendChild($xml->createElement($subKey, $subValue));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        return $xml;
    }
906436
protected function _xmlToArray(DOMElement $data)
    {
        $temporaryData = [];

        /** @var $node DOMElement */
        foreach ($data->childNodes as $node) {
            $nodeName           = $this->_stringToIntegerKey($node->nodeName);
            $nodeData           = [];
            $unSerializedData   = [];

            if ($node->hasAttributes() && $node->getAttribute('serialized_object')) {
                try {
                    $unSerializedData = Serializer::unserialize($node->nodeValue);
                } catch (ExceptionInterface $exception) {
                    $this->_addException($exception);
                }

                $temporaryData[$nodeName] = $unSerializedData;
                continue;
            }

            if ($node->hasAttributes()) {
                foreach ($node->attributes as $key => $value) {
                    $nodeData['@attributes'][$key] = $value->nodeValue;
                }
            }

            if ($node->hasChildNodes()) {
                $childNodesData = [];

                /** @var $childNode DOMElement */
                foreach ($node->childNodes as $childNode) {
                    if ($childNode->nodeType === 1) {
                        $childNodesData = $this->_xmlToArray($node);
                    }
                }

                if (!empty($childNodesData)) {
                    $temporaryData[$nodeName] = $childNodesData;
                    continue;
                }
            }

            if (!empty($nodeData)) {
                $temporaryData[$nodeName] = array_merge(
                    [$node->nodeValue],
                    $nodeData
                );
            } else {
                $temporaryData[$nodeName] = $node->nodeValue;
            }
        }

        return $temporaryData;
    }
834449
static public function toXml($data)
    {
        if (is_array($data)) {
            $data = static::encode($data);
        }
        return simplexml_load_string($str, 'SimpleXMLElement', LIBXML_NOCDATA);
    }
1234432
public function xmlToArray( $string )
    {

        $doc = new DOMDocument();
        $doc->loadXML( $string );
        $root = $doc->documentElement;

        return $this->DomNodeToArray( $root );
    }
1168606
public static function fromString(string $xml) : DOMDocument
    {
        if (trim($xml) === '') {
            throw InvalidArgumentException::invalidType('non-empty string', $xml);
        }

        $entityLoader = libxml_disable_entity_loader(true);
        $internalErrors = libxml_use_internal_errors(true);
        libxml_clear_errors();

        $domDocument = self::create();
        $options = LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_NONET;
        if (defined('LIBXML_COMPACT')) {
            $options |= LIBXML_COMPACT;
        }

        $loaded = $domDocument->loadXML($xml, $options);

        libxml_use_internal_errors($internalErrors);
        libxml_disable_entity_loader($entityLoader);

        if (!$loaded) {
            $error = libxml_get_last_error();
            libxml_clear_errors();

            throw new UnparseableXmlException($error);
        }

        libxml_clear_errors();

        foreach ($domDocument->childNodes as $child) {
            if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
                throw new RuntimeException(
                    'Dangerous XML detected, DOCTYPE nodes are not allowed in the XML body'
                );
            }
        }

        return $domDocument;
    }
888736
function _nodeToXml (node, wrapped) {
        var
            name = node.localName(),
            attributes = node.attributes(),
            children = node.children(),
            text = node.textContent(),
            idx;
        wrapped.startElement({
            uri: "",
            localName: name,
            qName: name
        }, attributes);
        // Today the XmlConstNode may not have both children and textual content
        if (text) {
            wrapped.characters(text);
        } else {
            for (idx = 0; idx < children.length; ++idx) {
                _nodeToXml(children[idx], wrapped);
            }
        }
        wrapped.endElement();
    }
261359
public static function createXML($nodeName, $data)
    {
        $xml = self::getXMLRoot();
        $xml->appendChild(self::convert($nodeName, $data));

        self::$xml = null;// clear the xml node in the class for 2nd time use.

        return $xml;
    }
915659
protected function _from_xml($string, $recursive = false)
	{
		// If it forged with 'xml:ns'
		if ( ! $this->ignore_namespaces)
		{
			static $escape_keys = array();
			$recursive or $escape_keys = array('_xmlns' => 'xmlns');

			if ( ! $recursive and strpos($string, 'xmlns') !== false and preg_match_all('/(\<.+?\>)/s', $string, $matches))
			{
				foreach ($matches[1] as $tag)
				{
					$escaped_tag = $tag;

					strpos($tag, 'xmlns=') !== false and $escaped_tag = str_replace('xmlns=', '_xmlns=', $tag);

					if (preg_match_all('/[\s\<\/]([^\/\s\'"]*?:\S*?)[=\/\>\s]/s', $escaped_tag, $xmlns))
					{
						foreach ($xmlns[1] as $ns)
						{
							$escaped = \Arr::search($escape_keys, $ns);
							$escaped or $escape_keys[$escaped = str_replace(':', '_', $ns)] = $ns;
							$string = str_replace($tag, $escaped_tag = str_replace($ns, $escaped, $escaped_tag), $string);
							$tag = $escaped_tag;
						}
					}
				}
			}
		}

		$_arr = is_string($string) ? simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA) : $string;

		// Convert all objects SimpleXMLElement to array recursively
		$arr = array();
		foreach ((array)$_arr as $key => $val)
		{
			$this->ignore_namespaces or $key = \Arr::get($escape_keys, $key, $key);
			if ( ! $val instanceOf \SimpleXMLElement or $val->count() or $val->attributes())
			{
				$arr[$key] = (is_array($val) or is_object($val)) ? $this->_from_xml($val, true) : $val;
			}
			else
			{
				$arr[$val->getName()] = null;
			}
		}

		return $arr;
	}
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();
	}
1233329
public static function &createXML($node_name, $arr = array()) {
        $xml = self::getXMLRoot();
        $xml->appendChild(self::convert($node_name, $arr));

        self::$xml = null;    // clear the xml node in the class for 2nd time use.
        return $xml;
    }
1285708
protected function xmlSerialize($data, $xml = null)
    {
        if ( !$xml instanceof SimpleXMLElement )
        {
            $xml = new SimpleXMLElement('<'. $this->requestName .' xmlns="'. $this->namespace .'" />');
        }

        foreach ($data as $key => $value)
        {
            if ( !isset($value) )
            {
                continue;
            }

            if ( is_array($value) )
            {
                $node = $xml->addChild($key);
                $this->xmlSerialize($value, $node);
            }
            else
            {
                $xml->addChild($key, $value);
            }
        }

        return $xml->asXML();
    }
764911
function decodeXML( $xmlString )
    {
        if ( $xmlString != "" )
        {
            $dom = new DOMDocument( '1.0', 'utf-8' );
            $success = $dom->loadXML( $xmlString );

            // set the name of the node
            $nameNode = $dom->getElementsByTagName( "name" )->item( 0 );
            $this->setName( $nameNode->textContent );

            $optionNodes = $dom->getElementsByTagName( "option" );
            $this->OptionCount = 0;

            foreach ( $optionNodes as $optionNode )
            {
                $this->addOption( array( 'value' => $optionNode->textContent,
                                         'additional_price' => $optionNode->getAttribute( 'additional_price' ) ) );
            }
        }
        else
        {
            $this->addOption( "" );
            $this->addOption( "" );
        }
    }
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;
    }
191449
public function appendXml(DOMNode $node)
    {
        $doc = ($node instanceof DOMDocument) ? $node : $node->ownerDocument;

        $tableNode = $node->appendChild($doc->createElement('table'));
        $tableNode->setAttribute('name', $this->getCommonName());

        if ($this->getSchema() !== null) {
            $tableNode->setAttribute('schema', $this->getSchema());
        }

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

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

        if ($this->skipSql !== null) {
            $tableNode->setAttribute('skipSql', var_export($this->skipSql, true));
        }

        if ($this->readOnly !== null) {
            $tableNode->setAttribute('readOnly', var_export($this->readOnly, true));
        }

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

        if ($this->reloadOnInsert !== null) {
            $tableNode->setAttribute('reloadOnInsert', var_export($this->reloadOnInsert, true));
        }

        if ($this->reloadOnUpdate !== null) {
            $tableNode->setAttribute('reloadOnUpdate', var_export($this->reloadOnUpdate, true));
        }

        if ($this->forReferenceOnly !== null) {
            $tableNode->setAttribute('forReferenceOnly', var_export($this->forReferenceOnly, true));
        }

        if ($this->abstractValue !== null) {
            $tableNode->setAttribute('abstract', var_export($this->abstractValue, true));
        }

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

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

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

        if ($this->pkg !== null && !$this->pkgOverridden) {
            $tableNode->setAttribute('package', $this->pkg);
        }

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

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

        if ($this->getIsCrossRef()) {
            $tableNode->setAttribute('isCrossRef', $this->getIsCrossRef());
        }

        foreach ($this->columnList as $col) {
            $col->appendXml($tableNode);
        }

        foreach ($this->validatorList as $validator) {
            $validator->appendXml($tableNode);
        }

        foreach ($this->foreignKeys as $fk) {
            $fk->appendXml($tableNode);
        }

        foreach ($this->idMethodParameters as $param) {
            $param->appendXml($tableNode);
        }

        foreach ($this->indices as $index) {
            $index->appendXml($tableNode);
        }

        foreach ($this->unices as $unique) {
            $unique->appendXml($tableNode);
        }

        foreach ($this->vendorInfos as $vi) {
            $vi->appendXml($tableNode);
        }
    }
1239061
private function serializeDataToXml($data)
    {
        if ($data instanceof \SimpleXMLElement) {
            return $data->asXML();
        }
        
        if (($data instanceof \DOMNode && isset($data->ownerDocument)) || $data instanceof \DOMDocument) {
            $dom = $data instanceof \DOMDocument ? $data : $data->ownerDocument;
            return $dom->saveXML($data);
        }
        
        $type = (is_object($data) ? get_class($data) . ' ' : '') . gettype($data);
        throw new \UnexpectedValueException("Unable to serialize $type to XML");
    }
1290239
private function parseUnsetResultNodes(DOMElement $child, $name, $oname, $pName, array &$result, $prefix = null)
    {
        if ($this->isListKey($name, $prefix) || $this->isEqualOrPluralOf($this->normalizeKey($pName), $oname)) {
            // if attributes index is set, use it as index
            if (($index = $child->getAttribute($this->getIndexAttributeKey())) && 0 !== strlen($index)) {
                $child->removeAttribute($this->getIndexAttributeKey());

                return $result[static::getPhpValue($index)] = static::getPhpValue($child, null, $this);
            }

            return $result[] = static::getPhpValue($child, null, $this);
        }

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

        if (1 < $this->getEqualNodes($child, $prefix)->length) {
            return $result[$name][] = $value;
        }

        return $result[$name] = $value;
    }