The main function for converting to an XML document.

@param  array|Traversable|string $data
@param  string   $root   if string then add "<?xml version='1.0' encoding='utf-8'?>" . <$root>
@return string   XML
171281
static public function encodeElement($data, $root='xml')
    {
        if (!$data) {
            $data = [];
        } elseif (is_string($data)) {
            return $data;
        }
        
        assert('is_array($data) || $data instanceof Traversable');
        
        $output = '';
        if ($data) foreach ($data as $key => $val) {
            if (is_numeric($key)) {
                // Convert the key to a valid string
                $key = "unknownNode_". (string) $key;
            }
            
            // Delete any char not allowed in XML element names
            $key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);
            
            if (is_array($val)) {
                $output .= '<' . $key . '>';
                $output .= self::encodeElement($val, null);
                $output .= '</' . $key . '>';
            } else {
                
                if (is_numeric($val)){
                    $output .= "<".$key.">".$val."</".$key.">";
                }else{
                    $val = str_replace('&', '&amp;', print_r($val, true));
                    $output .= "<". $key . "><![CDATA[".$val."]]></" . $key . ">";
                }
            }
        }
        if ($output) {
            $output = PHP_EOL . $output . PHP_EOL;
        }
        if ($root) {
            $output = "<?xml version='1.0' encoding='utf-8'?>" . PHP_EOL . "<$root>$output</$root>";
        }
        
        return $output;
    }
208584
public function array2xml($rootName="", $encoding="utf-8"){
		global $debug;
		$this->doc = new domdocument("1.0", $encoding);
		$arr = array();
		if (count($this->XMLArray) > 1){
			if ($rootName != ""){
				$root = $this->doc->createElement($rootName);
			}else{
				$root = $this->doc->createElement("root");
				$rootName = "root";
			}
			$arr = $this->XMLArray;
		}else{

			$key = key($this->XMLArray);
			$val = $this->XMLArray[$key];

			if (!is_int($key)){
				$root = $this->doc->createElement($key);
				$rootName = $key;
			}else{
				if ($rootName != ""){
					$root = $this->doc->createElement($rootName);
				}else{
					$root = $this->doc->createElement("root");
					$rootName = "root";
				}
			}
			$arr = $this->XMLArray[$key];
		}
		
		$root = $this->doc->appendchild($root);
	
		$this->addArray($arr, $root, $rootName);

/*		foreach ($arr as $key => $val){
			$n = $this->doc->createElement($key);
			$nText = $this->doc->createTextNode($val);
			$n->appendChild($nodeText);
			$root->appendChild($n);
		}
*/		
		
		return $this->doc->saveXML();
	}
458888
public function xml($data)
    {
        if (isset($data['url']) && ! empty($data['url'])) {
            $result = @file_get_contents($data['url']);
            $result = XmlFormatter::encode($result);
        }
        return isset($result) ? $result : null;
    }
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;
    }
745041
public function toXhtml(\DOMDocument $document, \DOMElement $root)
    {
        $content = '';
        $caption = '';

        foreach ($this->node->nodes as $node) {
            $content .= $node->token->content;
        }

        $matches = array();
        if (preg_match('/([^<]*)<?([^>]*)>?/', $content, $matches)) {
            // if the role uses the `caption<content>` notation; extract the two parts
            if (isset($matches[2]) && $matches[2]) {
                $content = $matches[2];
                $caption = trim($matches[1]);
            }
        }

        // check the table of contents for a caption.
        if (!$caption && $this->visitor) {
            $toc = $this->visitor->getTableOfContents();
            $caption = isset($toc[$content]) ? $toc[$content]->getName() : '';
        }

        // if no caption is captured; create one.
        if (!$caption) {
            $caption = str_replace(
                array('-', '_'),
                ' ',
                ucfirst(ltrim(substr(htmlspecialchars($content), strrpos($content, '/')), '\\/'))
            );
        }

        $link = $document->createElement('a', $caption);
        $root->appendChild($link);
        $link->setAttribute('href', str_replace('\\', '/', $content) . '.html');
    }
522547
public function assign(array $data, $root = 'Ctrl')
    {
        foreach ($data as $k => $d) {
            if ($root != '') {
                if (!isset($this->viewData[$root])) {
                    $this->viewData[$root] = [];
                }
                $this->viewData[$root][$k] = $d;
            } else {
                $this->viewData[$k] = $d;
            }

        }
    }
428364
private function getPathsData($data, string $root): ?array
    {
        if ($data === null || !\is_array($data) || \count($data) === 0) {
            return null;
        }

        return $this->existingPaths($data, $root);
    }
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;
    }
40894
private function createDocument($root = null)
    {
        $document = new \DOMDocument($this->version, $this->encoding);
        $document->formatOutput = $this->formatOutput;

        $this->node = $document->createElement($root ?: $this->root);
        $document->appendChild($this->node);

        return $document;
    }
485377
public static function arrayToXml($data, $root = null, $level = 1, $format = true)
    {
        if (empty($root)) {
            $root = config('df.xml_root', 'dfapi');
        }

        return '<?xml version="1.0" ?>' . static::arrayToXmlInternal($data, $root, $level, $format);
    }
1137832
public static Document mapToXml(Map<?, ?> data, String rootName) {
		final Document doc = createXml();
		final Element root = appendChild(doc, rootName);

		mapToXml(doc, root, data);
		return doc;
	}
666430
private function getXmlData($file)
    {
        /** @var \DOMDocument $xmlDomDocument */
        if (!$xmlDomDocument = XmlUtils::loadFile($file, __DIR__ . '/../xsd/schema.xsd')) {
            throw new \RuntimeException(sprintf(
                'Invalid schema xml file "%s".',
                $file
            ));
        }

        /** @var array $xmlData */
        if (!$xmlData = XmlUtils::convertDomElementToArray($xmlDomDocument->firstChild)) {
            throw new \RuntimeException('Invalid schema DOM object.');
        }

        $schemaId = SchemaId::fromString($xmlData['schema']['id']);

        $filePath = substr($file, 0, -strlen(basename($file)) - 1);
        $schemaPath = str_replace([':', '//'], ['/', '/'], $schemaId->getCurie());

        // invalid schema file location
        if (substr($filePath, -strlen($schemaPath)) !== $schemaPath) {
            throw new \RuntimeException(sprintf(
                'Invalid schema xml directory "%s". Expected sub-directory "%s".',
                $filePath,
                $schemaPath
            ));
        }

        // validate version to file
        if (basename($file) != 'latest.xml'
            && basename($file) != sprintf('%s.xml', $schemaId->getVersion()->toString())
        ) {
            throw new \RuntimeException(sprintf(
                'Invalid schema xml file "%s" name. Expected name "%s.xml".',
                $file,
                $schemaId->getVersion()->toString()
            ));
        }

        return $xmlData;
    }
1146647
protected function arrayToXml($data, \SimpleXmlElement $element, $parent = null)
    {
        foreach ($data as $key => $value) {
            if (is_array($value) || $value instanceof \Traversable) {
                if (!is_numeric($key)) {
                    if (count($value) > 0 && isset($value[0])) {
                        $this->arrayToXml($value, $element, $key);
                    } else {
                        $subnode = $element->addChild($key);
                        $this->arrayToXml($value, $subnode, $key);
                    }
                } else {
                    $subnode = $element->addChild($parent);
                    $this->arrayToXml($value, $subnode, $parent);
                }
            } else {
                if (!is_numeric($key)) {
                    if (substr($key, 0, 1) === '@') {
                        $element->addAttribute(substr($key, 1), $value);
                    } elseif ($key === 'value' and count($data) === 1) {
                        $element[0] = $value;
                    } elseif (is_bool($value)) {
                        $element->addChild($key, intval($value));
                    } else {
                        $element->addChild($key, htmlspecialchars($value, ENT_QUOTES));
                    }
                } else {
                    $element->addChild($parent, htmlspecialchars($value, ENT_QUOTES));
                }
            }
        }
    }
80889
public static function data_to_xml($data) {
        $xml = '';
        foreach ($data as $key => $val) {
            is_numeric($key) && $key = "item id=\"$key\"";
            $xml    .=  "<$key>";
            $xml    .=  ( is_array($val) || is_object($val)) ? self::data_to_xml($val)  : self::xmlSafeStr($val);
            list($key, ) = explode(' ', $key);
            $xml    .=  "</$key>";
        }
        return $xml;
    }
342046
public function paramTag($text, &$data, &$root)
    {
        $explode = preg_split('/[ \t]+/', $text);
        $type = array_shift($explode);
        if ($type) {
            $this->_var = trim(array_shift($explode), '$');
            if ($this->_var) {
                $data['parameters'][$this->_var]['type'] = $type;
            } else {
                $count = isset($data['parameters']) ? count($data['parameters']) : 0;
                $data['parameters']['__unknown'.$count]['type'] = $type;
            }
            $text = join(' ', $explode);
        }
        if ($text != '') {
            parent::tag('@param', $this->_var.' - '.$text, $root);
        } else {
            parent::tag('@param', NULL, $root);
        }
    }
1224279
private function getDocument($root = null)
    {
        return $this->document !== null ? $this->document : $this->document = $this->createDocument($root);
    }
987816
public XMLNode parseXML(String root) {
        if (xmlElementsMap.containsKey(root)) {
            return xmlElementsMap.get(root);
        }
        try {
            currentRoot = root;
            isParsing = false;
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            InputStream in = configuration.getBuilderXML();
            saxParser.parse(in, this);
            return xmlElementsMap.get(root);
        } catch (Throwable t) {
            t.printStackTrace();
            throw new DocletAbortException(t);
        }
    }