Log in

View Full Version : write xml file



lay
03-06-2009, 03:16 AM
<?php
function writeXML($xmlfile, $tablename)
{
$query= "SELECT * FROM $tablename";
$result = mysql_query($query);

$doc = new DOMDocument('1.0', 'UTF-8');

$root = $doc->createElement('TableName');
$doc->appendChild($root);

while ($row = mysql_fetch_assoc($result)) {
$root_child = $doc->createElement('Login');
$root->appendChild($root_child);

$root_attr1 = $doc->createAttribute('Id');
$root_child->appendChild($root_attr1);

$root_text = $doc->createTextNode($row['id']);
$root_attr1->appendChild($root_text);

$title = $doc->createElement( "Username" );
$title->appendChild(
$doc->createTextNode( $row['username'] )
);
$root_child->appendChild( $title );

$publisher = $doc->createElement( "Password" );
$publisher->appendChild(
$doc->createTextNode( $row['pwd'] )
);
$root_child->appendChild( $publisher );

$root->appendChild( $root_child );
}
//$myFile = "test.xml";
$fh = fopen($xmlfile, 'w') or die("can't open file");
fwrite($fh, $doc->saveXML());
}
?>
I have this function and i call it like this:
include_once("connection.php");

$link = openConnection();
$bd = SelectBD($link);
writeXML('sample.xml', 'userpwd');
It have the problem when the data in the database have the character special. The problems are in Firefox it display like this � and in IE it display like this :

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.

An invalid character was found in text content. Error processing resource 'file:///C:/Program Files/Apache Group/Apache2/ht...

<TableName><Login Id="1"><Username>titi</Username><Password>123456</Password></Login&...

if it don't have problem it must display like this :

<?xml version="1.0" encoding="UTF-8"?>
<TableName><Login Id="1"><Username>titi</Username><Password>123456</Password></Login><Login Id="2"><Username>éclajdkfjd</Username><Password>jjjjjjjjjjjjjh</Password>
</Login></TableName>

JasonDFR
03-06-2009, 07:43 AM
1) Make sure you database collation is UTF-8.

2) Unless you have a good reason to use DOM, don't. To make your xml file I would do it like this. Once you have the xml file you can always use it to create an instance of DOM if you need to.


<?php

// You can make a function out of this if you want
// Query Database here. Get your $result

$xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\r\n";

$xml .= '<tablename>' . "\r\n";

while ($row = mysql_fetch_assoc($result)) {

$xml .= '<login id="'. $row['id'] .'">' . "\r\n";
$xml .= '<username>' . $row['username'] . '</username>' . "\r\n";
$xml .= '<password>' . $row['password'] . '</password>' . "\r\n";
$xml .= '</login>' . "\r\n";

}

$xml .= '</tablename>';

$file= fopen('yourfile.xml', 'w');

fwrite($file, $xml);

fclose($file);

?>

lay
03-10-2009, 09:55 AM
Thank u very much for ur helping.
But after i try ur codes. It still have the same problem.

JasonDFR
03-10-2009, 10:04 AM
You still get this error?

An invalid character was found in text content. Error processing resource 'file:///C:/Program Files/Apache Group/Apache2/ht...

Could be a problem with the encoding type of the file that is being created. But I'm not sure.

If you figure it out, please post the solution.

Good luck.