All I needed to do was get up a get a drink of water and noticed what I was doing...
Sorry for wasting this space...
Anyway ...
New Code:
<?php
$xml_filename="this.xml";
if(!$xmlfile = fopen($xml_filename,"w")) {
print "Couldn't open file";
} else {
$f = mysql_connect("localhost","root","") or die("Database Down");
$res = mysql_db_query("mysql","SELECT * FROM user");
if(!$res) {
print "ERROR: " . mysql_error();
} else {
fputs($xmlfile,"<?xml version=\"1.0\" ?>\n");
fputs($xmlfile,"<result state=\"success\">\n");
while($row=mysql_fetch_array($res)) {
@fputs($xmlfile,"<row>\n");
for($i=0; $i < mysql_num_fields($res); $i++) {
@fputs($xmlfile,"\t<column>");
@fputs($xmlfile,$row[$i]."</column>\n");
}
@fputs($xmlfile,"</row>\n");
}
@fputs($xmlfile,"</result>");
@fclose($xmlfile);
}
}
print "XML File written...";
?>
For wasting space. Here is the script I'm using to parse the file.
<?php
print '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML lang="en">
<HEAD>
<TITLE>XML Parser</TITLE>
</HEAD>
<BODY>';
// Hacked this script from various snippets of code...
//handler for the start elements
function beginElement($parser, $name, $attribs)
{
if (strtolower($name) == "row")
{
//handler for the row element
print "<tr>";
}
if (strtolower($name) == "column")
{
//handler for the column
print "<td>";
}
if (strtolower($name) == "error")
{
//handler for the error
print "<tr><td>";
}
if (strtolower($name) == "result")
{
print "<br><table border=0 cellpadding=2 cellspacing=0>";
}
}
//handler for the end of elements
function endElement($parser, $name)
{
if (strtolower($name) == "row")
{
//handler for the row element
print "</tr>";
}
if (strtolower($name) == "column")
{
//handler for the column
print "</td>";
}
if (strtolower($name) == "error")
{
//handler for the error
print "</td></tr>";
}
if (strtolower($name) == "result")
{
print "</table> <br>";
}
}
//handler for character data
function characterData($parser, $data)
{
print "$data";
}
$xml_file = 'this.xml';
// declare the character set - UTF-8 is the default
$type = 'UTF-8';
// create our parser
$xml_parser = xml_parser_create($type);
// set some parser options
// enable case-folding for this parser
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
// set source encoding for this parser
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
//parse the XML
$xml_parser = xml_parser_create();
// set up start and end element handlers
xml_set_element_handler($xml_parser, "beginElement", "endElement");
// set up character data handler
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($xml_file, 'r'))) {
die("Could not open $xml_file for parsing!\n");
}
// loop through the file and parse!
while ($data = fread($fp, 4096)) {
if (!($data = utf8_encode($data))) {
echo 'ERROR'."\n";
}
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf( "XML error: %s at line %d\n\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
// Free this XML parser
xml_parser_free($xml_parser);
print '
</BODY>
';
?>
CT