George D.
George D.

Reputation: 1640

Getting the content of an xml file creates an XML Parsing Error

I am trying to get the contents of an XML file in case that the file exists or I am generating a new xml file. The problem is that when I am trying to get the xml file I get this error:

XML Parsing Error: no element found Location: http://mydomain.gr/generate.php Line Number 1, Column 1: ^

My code is this

<?php

include_once('xmlgenerator.php');
$xml = new XmlGenerator();
if($xml->cachefile_exists){
    if(!$xml->is_uptodate()){
        // echo "update it now";
        $xml->createFile = 1;
        $data = $xml->create();
        Header('Content-type: text/xml');
        print($data->asXML());
    }else{
        //echo "doesn't need any update.";
        Header('Content-type: text/xml');
        file_get_contents($xml->cached_file);
    }
}else{
   // echo "Didn't find any cache file. Lets Create it";
        $xml->createFile = 1;
        $data = $xml->create();
        Header('Content-type: text/xml');
        print($data->asXML());
}
?>

The XML structure is fine, and I double check about the XML file encoding or the php file that call the XML. Everything is UTF8 without BOM. When I open the XML file directly in a browsers it looks perfect and its a valid file ( checked it with w3c and online tools).

the 2 lines that create the problem(most probably):

Header('Content-type: text/xml');
file_get_contents($xml->cached_file) 

I even deleted anything and just used this 2 lines and got the same error.

So what can be wrong? Is there any proper way to include an XML file in a php file, change the headers and show it to the end user? I really don't want to redirect, I need to stay to the same php file just show XML content.

Upvotes: 0

Views: 213

Answers (1)

Matthew Scragg
Matthew Scragg

Reputation: 4638

echo file_get_contents($xml->cached_file) 

Upvotes: 1

Related Questions