XML with PHP “echo” getting error “Extra content at the end of the document”

前端 未结 3 1979
说谎
说谎 2020-12-12 06:16

I\'m getting an error that I believe to be very specific.

This PHP is stored in my domain. It access a server with a mysql database and uses it\'s table information

相关标签:
3条回答
  • 2020-12-12 06:43

    I had same problem and put ' ' around database and it worked.

    0 讨论(0)
  • 2020-12-12 06:59

    Just Right click your output error webpage and VIEW SOURCE CODE you will see the correct error message and line number from your PHP file. You will solve that error in few seconds.

    0 讨论(0)
  • 2020-12-12 06:59

    “Extra content at the end of the document”

    I would like to solve and understand why this happens.

    Why does this happen? This is in short an invalid XML. See the following example:

    <xml>
    </xml>
    This here is extra content at the end of the document
    

    As you can see, nobody would normally create such an XML file. In your case this happens because of a common accident of those programmers who outsmart themselves writing functions to output XML while those functions already exist. They just then forgot to properly output xml and then they are screwed:

    $xml = new SimpleXMLElement('<markers/>');
    
    foreach ($databaseResult as $row) 
    {
        $marker = $xml->addChild('marker');
        foreach ($row as $key => $value) 
        {
            $marker[$key] = $value;
        }
    }
    
    header("Content-type: text/xml");
    $xml->asXML('php://output');
    

    This example is using the SimpleXML library. If your database result is very large and you want to stream the data instead, you can take a look at the XMLWriter library:

    $writer = new XMLWriter();
    $writer->openUri('php://output');
    
    $writer->startDocument();
    $writer->startElement('markers');
    
    header("Content-type: text/xml");
    
    foreach ($databaseResult as $row)
    {
        $writer->writeRaw("\n  ");
        $writer->flush();
        $writer->startElement('marker');
    
        foreach ($row as $key => $value)
        {
            $writer->writeAttribute($key, $value);
        }
    
        $writer->endElement();
    }
    
    $writer->writeRaw("\n");
    $writer->endElement();
    $writer->flush();
    

    See it in action.

    0 讨论(0)
提交回复
热议问题