Reputation: 7429
I have written a PHP script to write an XML document but there is an error:
<?php
$db = "unadb";
$connection = mysql_connect("localhost", "root", "") or die("Could not connect.");
$table_name = 'article';
$db = mysql_select_db($db, $connection);
$query = "select * from " . $table_name;
$result = mysql_query($query, $connection) or die("Could not complete database query");
$num = mysql_num_rows($result);
if ($num != 0) {
$file= fopen("results.xml", "w");
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
$_xml .="<atricle>";
while ($row = mysql_fetch_array($result)) {
$_xml .="\t<page>\r\n";
if ($row["title"]) {
$_xml .="\t<page title=\"" . $row["title"] . "\">\r\n";
$_xml .="\t<source>" . $row["source"] . "</source>\r\n";
$_xml .="\t<kw>" . $row["kw"] . "</kw>\r\n";
$_xml .="\t\t<Url>" . $row["url"] . "</Url>\r\n";
$_xml .="\t\t<author>" . $row["author"] . "</author>\r\n";
$_xml .="\t<status>" . $row["status"] . "</status>\r\n";
$_xml .="\t</page>\r\n";
} else {
$_xml .="\t<page title=\"Nothing Returned\">\r\n";
$_xml .="\t\t<file>none</file>\r\n";
$_xml .="\t</page>\r\n";
} }
$_xml .="</atricle>";
fwrite($file, $_xml);
fclose($file);
echo "XML has been written. <a href=\"results.xml\">View the XML.</a>";
} else {
echo "No Records found";
} ?>
The output is:
<?xml version="1.0" encoding="UTF-8" ?>
<atricle> <page>
<page title="Coming Race">
<source>http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml</source>
<kw>home</kw>
<Url>http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332</Url>
<author>afnan</author>
<status>active</status>
</page>
<page>
<page title="Coming Race">
<source>http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml</source>
<kw>home</kw>
<Url>http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332</Url>
<author>Doctor</author>
<status>active</status>
</page>
</atricle>
I think the XML is OK but there is an error:
error on line 18 at column 11: Opening and ending tag mismatch: page line 0 and atricle
Upvotes: 1
Views: 551
Reputation: 4536
As everyone else has said, the double pages are doing you in. Also, while it's not causing you any problems, your overall container is "atricle" and not "article".
Upvotes: 0
Reputation: 47203
Your output won't work since you're duplicating the <page>
element. This should be your output (I've commented what you should omit):
<?xml version="1.0" encoding="UTF-8" ?>
<atricle>
<!-- <page> -->
<page title="Coming Race">
<source>http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml</source>
<kw>home</kw>
<Url>http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332</Url>
<author>afnan</author>
<status>active</status>
</page>
<!-- <page> -->
<page title="Coming Race">
<source>http://www.public-domain-content.com/books/Coming_Race/C1P1.shtml</source>
<kw>home</kw>
<Url>http://www.uberarticles.com/articles/artsubmit/upreview.php?article_id=580332</Url>
<author>Doctor</author>
<status>active</status>
</page>
</atricle>
Upvotes: 0
Reputation: 91983
You have a total of four <page>
opening tags, but only two </page>
closing ones.
Upvotes: 0
Reputation: 86446
Remove this line of code $_xml .="\t<page>\r\n";
while ($row = mysql_fetch_array($result)) {
$_xml .="\t<page>\r\n";
To
while ($row = mysql_fetch_array($result)) {
Upvotes: 2
Reputation: 49331
You're creating XML with raw string manipulation. Getting the open and close tags to match (you have two open <page>
tags for each close </page>
is going to be the first of many problems with escaping the SQL results you're dumping straight into the XML.
Use a library, or you will have to do a very large amount of work to fix lots of corner cases.
Upvotes: 3