Error in classxml

Asked

Viewed 133 times

0

Could someone tell me how to fix this mistake?

inserir a descrição da imagem aqui

Acessarserver.php file:

<?php
$xml = simplexml_load_file("server.php?id=8");
echo $xml;

File server.php:

<?php 
include("classxml.php");

$xml = new Xml();

$erro = 0;

$idproduto = $_GET['id'];

$xml->openTag('response');

if($idproduto=='') {

    $erro = '1';
    $msg  = 'codigo invalido!';
} else {

    $erro = '2';
    $msg  = 'codigo valido!';
}
$xml->addTag('erro',$erro);
$xml->addTag('msg',$msg);
$xml->closeTag('response');

echo $xml;

classxml.php file:

<?php

/**
 * Description of XmlClass
 *
 * @author Rafael
 */
class Xml {

    private $xml;
    private $tab = 1;
    public function __construct($version="1.0", $encoding="UTF-8") {
        $this->xml .= "<?xml version='$version' encoding='$encoding'?>\n";
    }

    public function openTag($name){
        $this->addTab();
        $this->xml .= "<$name>\n";
        $this->tab++;
    }

    public function closeTag($name){
        $this->tab--;
        $this->addTab();
        $this->xml .= "</$name>\n";        
    }

    public function addTag($name, $value){
        $this->addTab();
        $this->xml .= "<$name>$value</$name>\n";
    }

    private function addTab(){
        for ($i = 1; $i <= $this->tab; $i++){
            $this->xml .= "\t";
        }
    }

    public function __toString() {
        return $this->xml;
    }
}

1 answer

1

Your XML file is invalid, that is, it has syntax error. To check whether the file is valid or not, pass it in an XML validator like this http://www.xmlvalidation.com/

  • The error is at the beginning of the class. But I already searched a lot and I couldn’t find the solution. ERROR(Content is not allowed in Prolog) help people!

  • Did you at least pass the file in the validator? Save the XML file and validate if the format generated by the class is correct.

  • Another thing: the server.php file has an error, you have q inform q the return is XML by putting the following code in the first line of the server.php: header("Content-type: text/xml; charset=utf-8");

  • I passed Filipe. The error(Content is not allowed in Prolog) was in this line: $this->xml .= " <? xml version='$version' encoding='$encoding'? > n";. I passed another validator and the error was this:Error in XML processing: incorrect formatting Row number 7, column 71: $this->xml .=" <? xml version='$version' encoding='$encoding' ? > n";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.