1

I'm having two errors, can't get this to work...

With the code like this:

<html>
    <body>
        <?php
        /*
         Fontes:
         - http://blog.clares.com.br/gerando-xml-com-php/
         - https://forum.imasters.com.br/topic/482006-resolvido%C2%A0gerar-nomes-aleat%C3%B3rios/
         */
        header('Content-type: text/xml; charset=utf-8');

        if(empty($_POST["Pergunta"])) {
            $pergunta = "";
            echo "dar um alert e voltar";
        } else {
            $pergunta = $_POST["Pergunta"];
        }
        if(empty($_POST["Resposta"])) {
            $resposta = "";
            echo "dar um alert e voltar";
        } else {
            $resposta = $_POST["Resposta"];
            $teste = explode("\n",$resposta);
            echo '<pre>' . print_r($teste) . '</pre>';
        }
        /* CHECKBOX
         if(isset($_POST['MostrarAIML'])){
         //echo "checkbox marcado! <br/>";
         //echo "valor: " . $_POST['MostrarAIML'];
         $MostrarAIML = true;
         }else{
         //echo "checkbox não marcado! <br/>";
         $MostrarAIML = false;
         }

         if(isset($_POST['SalvarAIML'])){
         //echo "checkbox marcado! <br/>";
         //echo "valor: " . $_POST['SalvarAIML'];
         $SalvarAIML = true;
         }else{
         //echo "checkbox não marcado! <br/>";
         $SalvarAIML = false;
         } */
        if(empty($_POST["forma"])) {
            $escolhe = "MostrarAIML";
        } else if($_POST["forma"]=='MostrarAIML'){
            $escolhe = "MostrarAIML";
        } else if($_POST["forma"]=='SalvarAIML'){
            $escolhe = "SalvarAIML";
        }

        function nome_aleatorio(){
            $tamanho = mt_rand(5,9);
            $all_str = "abcdefghijlkmnopqrstuvxyzwABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
            $nome = "";
            for ($i = 0;$i <= $tamanho;$i++){
                $nome .= $all_str[mt_rand(0,61)];
            }
            return $nome;
        }


        # versao do encoding xml
        $dom = new DOMDocument("1.0", "UTF-8");

        # retirar os espacos em branco
        $dom->preserveWhiteSpace = false;

        # gerar o codigo
        $dom->formatOutput = true;

        # criando o nó principal (root)
        $root = $dom->createElement("aiml");

        $comentario = $dom->createComment("Copyright ©2017 - FAST.aiml
         URL: http://avatar.cinted.ufrgs.br/alunos/fabricio/fastaiml/
         Powered by Fabricio Herpich < fabricio_herpich at hotmail.com >
         Project coordinator: Liane Margarida Rockenbach Tarouco < liane at penta.ufrgs.br >");

        # nó filho (category)
        $category = $dom->createElement("category");
        # nó filho (pattern) / #setanto a pergunta
        $pattern = $dom->createElement("pattern", $pergunta);
        # nó filho (template) / #setanto a resposta
        $template = array();

        for($i = 0; $i < sizeof($teste); $i++){
            $template[$i] = $dom->createElement("template", $teste[$i]);
        }

        # adiciona os nós em aiml
        $category->appendChild($comentario);
        $category->appendChild($pattern);

        for($i = 0; $i < sizeof($template); $i++){
            $category->appendChild($template[$i]);
        }

        # adiciona o nó contato em (root) agenda
        $root->appendChild($category);
        $dom->appendChild($root);



        # busco um nome aleatório
        $nome_arquivo = nome_aleatorio();

        $dom->save("backup__/" . $nome_arquivo . ".xml");




        if($escolhe == "MostrarAIML"){
            # imprime o xml na tela
            print $dom->saveXML();

        }

        if($escolhe == "SalvarAIML"){
            # imprime o xml na tela
            print $dom->saveXML();
            # salva o arquivo AIML.xml
            header("Content-Disposition: attachment; filename=fastaiml_" . $nome_arquivo .  ".xml");
        }

        ?>
    </body>
</html>

I'm getting this error:

This page contains the following errors: error on line 12 at column 18: XML declaration allowed only at the start of the document Below is a rendering of the page up to the first error.**

I have other php file that works perfectly, where the header() function is after the $nome_arquivo = nome_aleatorio(); But, In this case I don't have the for loops on the code, 'cause I only have one for the file...and in this case I have to put multiple inside the tag...

If I put the header() like the other file, I get:

This page contains the following errors: error on line 7 at column 18: XML declaration allowed only at the start of the document Below is a rendering of the page up to the first error.**

how to work around this?

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
alvarosps
  • 83
  • 1
  • 11
  • All headers (including your Content-Type) must be declared before any output is made at all (including your HTML-tags). – M. Eriksson Aug 24 '17 at 13:33
  • You're also printing the XML inside a HTML-document. Remove your HTML tags at the top and at the bottom. – M. Eriksson Aug 24 '17 at 13:39

0 Answers0