Jump to content
Larry Ullman's Book Forums

Recommended Posts

As part of a larger project, I'm trying to generate an xml file using xmlWriter. The file is created and looks okay but I get an error - "This page contains the following errors: error on line 1 at column 1: Document is empty Below is a rendering of the page up to the first error." The rest of the page is blank.

 

The source code only displays my error message "Problems creating file "fcTeams.xml".

 

I've include a simplified version of my code which is where the error is coming from. Any ideas what's causing the error?

$xml = new XMLWriter();
$xml->openUri('fcTeams.xml');
$xml->setIndent(true);
$xml->setIndentString("\t");
$xml->startDocument();
$xml->startElement('teams');

$xml->startElement('fc');
$xml->writeAttribute('id', '03');
$xml->writeElement('name', 'Real Madrid');
$xml->writeElement('manager', 'Jose Mourinho');
$xml->writeElement('captain', 'Iker Casillas');
$xml->writeElement('stadium', 'Bernabeu');

$xml->startElement('player');
$xml->writeElement('name' , 'Christiano Ronaldo');
$xml->writeElement('position','forward');
$xml->writeElement('number','7');
$xml->writeElement('country','Portugal');
$xml->endElement();

$xml->startElement('player');
$xml->writeElement('name' , 'Kaka');
$xml->writeElement('position','midfielder');
$xml->writeElement('number','8');
$xml->writeElement('country','Brazil');
$xml->endElement();

$xml->endElement();

$xml->endElement();
$xml->endDocument();
header ('Content-type: text/xml');
if ($xml->flush() ) {
echo 'File "fcTeams.xml" created.';
}
else {
echo 'Problems creating file "fcTeams.xml".';
}

Link to comment
Share on other sites

I have to ask this: Does this really make sense? You are generating a whole XML file by PHP here. Could it possibly be done simpler? Have you checked out JSON? CVS? If you really need the integrity good XML can offer, then ignore this part.

 

Look at your error message. Does the file exist? Is the directory writable? Some simple checks you have to do here. It says "problem creating file". It would indicate the file is not found for me.

 

Do you like football, margaux, or is this just a project? Forza Juve!

Link to comment
Share on other sites

No, this doesn't make sense but if I were to post all the code that got me to this point I don't think anybody would want to look through it. The larger project is to create an rss feed from a database.

I posted the previous code as I have narrowed down that the error I'm getting is in the generateXMl class - which incidentally doesn't have all this xml, it uses several foreach loops after retrieving alot more data from a d/b.

 

So - can you help me? If I run the code I already posted in the browser, this is what I get.

"This page contains the following errors: error on line 1 at column 1: Document is empty Below is a rendering of the page up to the first error."
The rest of the page is blank.

 

The source code only displays my error message

"Problems creating file "fcTeams.xml".

 

There is no php syntax error and most of the logic is working as the file is created. I wonder if its an xml error? an encoding error? a version error? I don't know enough about xml to know and I have done a number of searches but have not found anything to indicate what might be causing the error.

 

and yes I love football, this is a project to try to create something using oop.

Link to comment
Share on other sites

Thanks Larry, yes I am.

 

Not sure what to try or what further information to give that would help to debug. I think the cause is something to do with

$xml->flush()

The file is being created and looks good, proper format and populated with the data as I would expect. Search results yielded very little information, one thread mentioned that the error was an xml error, but as nothing is displayed and the error message states that

the error begins on line 1 column 1

I'm at a loss. Any information you can contribute would be great.

Link to comment
Share on other sites

This is new to me and I did not know to do that. I found a site that validates xml files and my file is 'invalid'. How do I define the file as of a particular schema and how does the xml file "find its scheme"? My project file is an rss file and is of the format

<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>mol rss feed</title>
<description>mol articles</description>
<link>http://www.mol.co.uk/articles.xml</link>
<lastBuildDate>Sat, 15 Dec 2012 17:24:05 +0000</lastBuildDate>
<item>
<title>Article 1</title>
<description>Description of Article 1</description>
<pubDate>Tue, 05 Jun 2012 09:58:19 +0100</pubDate>
<link>http://www.mol.co.uk/blog.php?id=4</link>
</item>
</channel>
</rss>

Link to comment
Share on other sites

Thanks for sticking with this. Several messages are output:

 

Well Formed: VALID

Schema Validation: INVALID

cvc-elt.1.a: Cannot find the declaration of element 'rss'.

 

From what I've been able to find out online I've included the required items and attributes for declaring the schema as RSS but obviously I am missing something. Any suggestions?

Link to comment
Share on other sites

  • 2 weeks later...

Okay, so because you're not providing a schema reference, the validator marks it as invalid. But you don't have to provide a schema reference for RSS, because it's already built into the apps that support it. That RSS syntax looks right to me. Just to confirm, this is exactly the output that's being created?

Link to comment
Share on other sites

Below is the file that was run recently. I think the format is correct but maybe I'm missing something?

<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>mol rss feed</title>
<link>http://www.mol.co.uk/articles.xml</link>
<description>Articles from mol</description>
<lastBuildDate>Thu, 03 Jan 2013 21:14:48 +0000</lastBuildDate>
<item>
<title>Responsive Design</title>
<description>Adapting website design to all internet devices</description>
<pubDate>Tue, 05 Jun 2012 09:58:19 +0100</pubDate>
<link>http://www.mol.co.uk/blog.php?id=4</link>
</item>
<item>
<title>New EU Cookie Law</title>
<description>From 22nd May you better be compliant</description>
<pubDate>Tue, 01 May 2012 09:56:09 +0100</pubDate>
<link>http://www.mol.co.uk/blog.php?id=2</link>
</item>
<item>
<title>Gamification</title>
<description>The new trend in retail marketing</description>
<pubDate>Tue, 03 Apr 2012 09:58:00 +0100</pubDate>
<link>http://www.mol.co.uk/blog.php?id=3</link>
</item>
<item>
<title>More on SEO</title>
<description>Common misunderstandings about SEO</description>
<pubDate>Tue, 06 Mar 2012 12:11:44 +0000</pubDate>
<link>http://www.mol.co.uk/blog.php?id=6</link>
</item>
<item>
<title>ARIA</title>
<description>Why you should ensure your website is acessible</description>
<pubDate>Tue, 07 Feb 2012 12:03:23 +0000</pubDate>
<link>http://www.mol.co.uk/blog.php?id=5</link>
</item>
<item>
<title>Cloud Computing</title>
<description>The new buzzword in internet computing - What is i</description>
<pubDate>Tue, 03 Jan 2012 09:56:00 +0000</pubDate>
<link>http://www.mol.co.uk/blog.php?id=1</link>
</item>
</channel>
</rss>

Link to comment
Share on other sites

That looks okay to me. To go back a couple of steps, could you clarify or restate where the problem is? The code you've posted outputs an XML file to be used as an RSS feed. Is the problem in the creation of the file or is it in the use of the file? And if it's the latter, please elaborate on that.

Link to comment
Share on other sites

Well I'm not sure if there is a problem in that I found this http://drupal.org/node/1435264. The first comment indicates a problem that may be what I'm experiencing - The documentation for XMLWriter::flush will return the number of bytes written if opened using URI and apparently it is possible for the number of bytes written with a flush to be zero. I dont completely understand this but maybe that is what I'm getting. When I did a var_dump on the result of my flush, it was int(0) as opposed to bool(False) which is what I would expect.

 

Going back to the code I posted originally I changed the end to

[/size][/font]
[font='Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif][size=3]$xml->endElement();
$xml->endDocument();
header ('Content-type: text/xml');
$result = $xml->flush();[/size][/font]
[font='Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif][size=3]if ($result) {
echo 'File "fcTeams.xml" created.';
}
else {
echo 'Problems creating file "fcTeams.xml".';
}
Link to comment
Share on other sites

Well I'm not sure if there is a problem in that I found this http://drupal.org/node/1435264. The first comment indicates a problem that may be what I'm experiencing - The documentation for XMLWriter::flush will return the number of bytes written if opened using URI and apparently it is possible for the number of bytes written with a flush to be zero. I dont completely understand this but maybe that is what I'm getting. When I did a var_dump on the result of my flush, it was int(0) as opposed to bool(False) which is what I would expect.

 

Going back to the code I posted originally I changed the end to

[/size][/font]
[font='Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif][size=3]$xml->endElement();
$xml->endDocument();
header ('Content-type: text/xml');
$result = $xml->flush();[/size][/font]
[font='Lucida Grande', 'DejaVu Sans', 'Bitstream Vera Sans', Verdana, Arial, sans-serif][size=3]if ($result) {
echo 'File "fcTeams.xml" created.';
}
else {
echo 'Problems creating file "fcTeams.xml".';
}
Link to comment
Share on other sites

Sorry for posting the above a few times - Not sure what happened here, when I tried to edit the post, it just posted it again and added in a bunch of font tags.

 

Anyway, have you ever come across this side effect from xmlwriter->flush? Do you have any suggestions as to how I would check the status of writing the file?

 

Thanks.

Link to comment
Share on other sites

Here's what's confusing me: if this script creates RSS (XML) output, then it needs the header() but the echo statement will cause errors. If this script creates an XML file, then the echo is fine but the header() is not.

Link to comment
Share on other sites

 Share

×
×
  • Create New...