Return the XML with correct indentation.

@param string $xml
@return string
@throws Exception
4571
public function format($xml)
    {
        $dom = new DOMDocument('1.0');
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $dom->loadXML($xml);
        $formatted = $dom->saveXML();

        if (!$formatted) {
            throw new Exception("An error occurred while formatting the XML");
        }

        return $formatted;
    }
455602
public function transformToHTML($xml)
    {
        if (is_string($xml)) {
            $dom = new DOMDocument();
            $dom->load($xml);
        } else {
            $dom = $xml;
        }
        $out = $this->xsltProcessor->transformToXML($dom);

        // fudges for HTML backwards compatibility
        // assumes that document is XHTML
        $out = str_replace('/>', ' />', $out); // <br /> not <br/>
        $out = str_replace(' xmlns=""', '', $out); // rm unnecessary xmlns

        if (class_exists('Tidy')) {
            // cleanup output
            $config = array(
                'indent'        => true,
                'output-xhtml'  => true,
                'wrap'          => 80
            );
            $tidy = new Tidy;
            $tidy->parseString($out, $config, 'utf8');
            $tidy->cleanRepair();
            $out = (string) $tidy;
        }

        return $out;
    }
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;
    }
518558
public function toArray( $xml )
	{
		$this->data	= array();
		$this->xml	= xml_parser_create();
		xml_set_object( $this->xml, $this );
		xml_set_element_handler( $this->xml, 'handleTagOpenForArray', 'handleTagCloseForArray' );
		xml_set_character_data_handler( $this->xml, 'handleCDataForArray' );
		$this->last	= array( &$this->data );
		if( !xml_parse( $this->xml, $xml ) )
		{
			$msg	= "XML error: %s at line %d";
			$error	= xml_error_string( xml_get_error_code( $this->xml ) );
			$line	= xml_get_current_line_number( $this->xml );
			throw new RuntimeException( sprintf( $msg, $error, $line ) );
		}
		xml_parser_free( $this->xml );
		return $this->data;
	}
1066810
public function toXmlLines($indenter = '  ')
    {
        if ($this->isSkip()) {
            $result = null;
        } else {
            $nsn = static::MULTILINGUAL_NAMESPACE_NAME;
            $hreflang = h($this->getFinalHrefLang());
            $href = h((string) $this->getUrl());
            $result = [
                "{$indenter}{$indenter}<{$nsn}:link rel=\"alternate\" hreflang=\"{$hreflang}\" href=\"{$href}\" />",
            ];
        }

        return $result;
    }
934499
public function createResponseFromXml($xml)
    {
        $dom = new \DomDocument($xml);
        if (!$dom->loadXML($xml)) {
            return;
        }

        $data = array();

        foreach($dom->childNodes as $root) {
            if ($root->nodeType !== XML_ELEMENT_NODE) {
                continue;
            }
            foreach($root->childNodes as $node) {
                $name = $node->nodeName;
                $text = $node->textContent;
                $data[$name] = $text;
            }
        }

        return new Response($data);
    }
185996
public static function stringToDOMDocument($xml, $filename = '')
    {
        $preUseInternalErrors = libxml_use_internal_errors(true);
        try {
            libxml_clear_errors();
            $doc = new DOMDocument();
            if (@$doc->loadXML($xml) === false) {
                throw new Exception(empty($filename) ? 'Failed to parse xml' : ('Failed to load file '.$filename));
            }
        } catch (Exception $x) {
            libxml_use_internal_errors($preUseInternalErrors);
            libxml_clear_errors();
            throw $x;
        }
        libxml_use_internal_errors($preUseInternalErrors);

        return $doc;
    }
722430
protected static function getXsltDOMDocument()
    {
        static $cache = null;
        $xsltFile = __DIR__.'/doctrine-xml-0.5.xsl';
        if (!isset($cache)) {
            $s = (is_file($xsltFile) && is_readable($xsltFile)) ? @file_get_contents($xsltFile) : false;
            if ($s === false) {
                throw new Exception('Failed to load the xslt file '.$xsltFile);
            }
            $cache = $s;
        }

        return Utilities::stringToDOMDocument($cache, $xsltFile);
    }
515805
public function getXmlResponse($key)
    {
        $xml =<<<EOF
<!--?xml version="1.0" encoding="ISO-8859-1"?-->
<getautomb_key>
    <ep_status>ok0</ep_status>
    <ep_message>generated document</ep_message>
    <ep_cin>%s</ep_cin>
    <ep_user>%s</ep_user>
    <ep_doc>%s</ep_doc>
    <ep_key>%s</ep_key>
</getautomb_key>
EOF;

        return trim(sprintf($xml, $this->cin, $this->user, $this->doc, $key));
    }
445345
public function Init($xml)
    {
        $this->FromXml($xml);
        //fix bug 2015-06-29
        if ($this->values['return_code'] != 'SUCCESS') {
            return $this->GetValues();
        }
        $this->CheckSign();
        return $this->GetValues();
    }
840835
public static function indent($xml) {
      $cnt = 0;
      $d = 0;
      $t = 0;
      $c = 0;
      while ($cnt != strlen($xml)) {
        $ch = substr($xml, $cnt, 1);
        $ch2 = substr($xml, $cnt, 2);
        $lch = substr($xml, strlen($xml) - 1, 1);
        $f = strlen($xml);
        $a = strlen($xml);

          if ($ch == "<" && $ch2 != "</" && $cnt != "0") {
            $n = "\n";
              for($i = 0; $i <= ($t - $c); $i ++) {
                $n .= "\t";
              }

            $pre = substr($xml, 0, $cnt);
            $af = substr($xml, $cnt, (max(strlen($xml), $cnt) - min(strlen($xml), $cnt)));
            $xml = $pre . $n . $af;

            $a = strlen($xml);
            $d ++;
            $t ++;
          }

          if ($ch2 == "</") {
            $n = "\n";
            for($i = 0; $i <= ($t - $c) - 1; $i ++) {
              $n .= "\t";
            }

            $pre = substr($xml, 0, $cnt);
            $af = substr($xml, $cnt, (max(strlen($xml), $cnt) - min(strlen($xml), $cnt)));
            $xml = $pre . $n . $af;

            $a = strlen($xml);
            $c ++;
          }

          $cnt += ($a - $f) > 0 ? ($a-$f)+1:1;         
      }

      return $xml;
    }
1265325
public static function unparse($xml)
	{
		if (isset(static::$beforeUnparse))
		{
			$xml = call_user_func(static::$beforeUnparse, $xml);
		}

		$text = Unparser::unparse($xml);

		if (isset(static::$afterUnparse))
		{
			$text = call_user_func(static::$afterUnparse, $text);
		}

		return $text;
	}
172368
public static function parseElementXml(SimpleXMLElement $xml){

        $attributes = array();
        foreach($xml->attributes() as $name => $value){
            $attributes[$name] = (string) $value;
        }

        $returnValue = array(
            'qtiClass' => $xml->getName()
        );

        if(count($attributes)){
            $returnValue['attributes'] = $attributes;
        }

        return $returnValue;
    }
1226439
public function to_xml($data){
        if(!is_array($data) || count($data) <= 0) throw new \Exception('xml data exception!');
        $xml = "<xml>";
        foreach ($data as $key=>$val){
            if (is_numeric($val)){
                $xml.="<".$key.">".$val."</".$key.">";
            }else{
                $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";
        return $xml;
    }
247557
private function createFieldsElement($xml)
    {
        $fields = $xml->createElement('fields');

        foreach ($this->fields as $f) {
            $fields->appendChild($this->createFieldElement($xml, $f));
        }

        return $fields;
    }
281571
public static function arrayFromXml($xml)
    {
        $document = new DOMDocument('1.0', 'UTF-8');
        $document->loadXML($xml);

        $root = $document->documentElement->nodeName;

        return Util::delimiterToCamelCaseArray([
            $root => self::_nodeToValue($document->childNodes->item(0)),
        ]);
    }
665540
public function render($xml)
	{
		if (substr($xml, 0, 3) === '<t>' && substr($xml, -4) === '</t>')
		{
			return $this->renderPlainText($xml);
		}
		else
		{
			return $this->renderRichText(preg_replace('(<[eis]>[^<]*</[eis]>)', '', $xml));
		}
	}