1

As the title says, I've been trying to wrap my head around this error for the past couple of hours. I can't seem to find anything wrong with the code and I have compared it to several other examples that reportedly work.

Here is my code:

    $q_export= $pdo->query("SELECT * FROM Produse");

    $string="<produs><br />";   


    while($results_exp= $q_export->fetch(PDO::FETCH_ASSOC))
    {
    $IDexp=$results_exp['ID'];
    $DenExp=$results_exp['Denumire'];
    $DescrExp=$results_exp['Descriere'];
    $PretEx=$results_exp['Pret'];
    $string .= "<ID>".$IDexp."</ID>"."<br />";
    $string .= "<Denumire>".$DenExp."</Denumire>"."<br />";
    $string .="<Descriere>".$DescrExp."</Descriere>"."<br />";
    $string .="<Pret>".$PretEx."</Pret>"."<br />";
    }

    $string .="</produs>";
    $xml_export= new SimpleXMLElement($string);
    Header('Content-type:text/xml');
    echo $xml_export->asXML();

And the error it gives is:

    This page contains the following errors:

    error on line 9 at column 1: Extra content at the end of the document
    Below is a rendering of the page up to the first error.

I'd greatly appreciate any input! :)

CryptoHero
  • 33
  • 1
  • 6
  • can you give the output? is the error given on the page you are displaying or in the error logs? – bkdude Jul 09 '14 at 00:18
  • It's being given on the page I'm displaying. – CryptoHero Jul 09 '14 at 00:28
  • Have you looked at http://stackoverflow.com/questions/8850120/extra-content-at-the-end-of-the-document-php-and-xml to make sure you dont have any extra characters lurking at the end? – Mark Silverberg Jul 09 '14 at 00:40
  • Also take a look at http://stackoverflow.com/questions/16026944/xml-with-php-echo-getting-error-extra-content-at-the-end-of-the-document . To debug, can you maybe paste the `$string` before you pass it into `SimpleXMLElement` – Mark Silverberg Jul 09 '14 at 00:42
  • try turning the compression to off.. – Randy Jul 09 '14 at 00:51
  • Do not create XML by concatenating strings, instead use an XML Library like SimpleXML to create the document (empty), then add the elements. The library will take care that the document is in correct state and in the right encoding. Like bkdude has shown: http://stackoverflow.com/a/24643882/367456 – hakre Jul 11 '14 at 20:04

2 Answers2

1

I don't think the XML is being created properly. you can try to output the string directly (which I don't think will work but it would be more correct than trying to parse it into a simpleXML object.)

try something along the lines of this. (probably going to have bugs but you will get the idea)

$q_export= $pdo->query("SELECT * FROM Produse");

$xml_export = new SimpleXMLElement('<xml/>');

while($results_exp= $q_export->fetch(PDO::FETCH_ASSOC))
{
    $record= $xml->addChild('produs');
    $record->addChild('ID',$results_exp['ID']);
    $record->addChild('Denumire',$results_exp['Denumire']);
    $record->addChild('Descriere',$results_exp['Descriere']);
    $record->addChild('Pret',$results_exp['Pret']);

}
Header('Content-type:text/xml');
echo $xml_export->asXML();
bkdude
  • 201
  • 1
  • 6
0

Would it possible for you to post a sample error XML output, and the entire XML/HTML of that particular page in source code? It'd help a lot to have these information.

I'm guessing your XML tags are being closed by an injection attack:

<produs><br />
 <!--Trouble! Someone's got the ID "MyID</ID></produs>"-->
 <ID>MyID</ID></produs><br /></MyID>
 <!--Rest of your XML output-->
</produs>

For a quick fix, apply a sanitizer function like htmlspecialchars() to user data before concatenation.

"<ID>".htmlspecialchars($IDExp)."</ID>"
Preole
  • 1
  • 2