...

View Full Version : Help me better understand XML



weronpc
03-08-2004, 02:05 AM
I created a xml file called text.xml
I created a xsl file called text.xsl

I open the browser localhost/text.xml and everything is fine.

My question is, I want to use php to retrieve my data from mysql server. then put the data into xml file. so, I have to generage a xml file with the data I just retrieved. What if there is 10 users visiting my site. do I have to produce 10 xml files?

I know there is a better way to do that, I just don't know how.

sad69
03-09-2004, 12:16 AM
I don't know much about PHP/XML, but I do know about PHP/HTML, and I'm assuming it's the same idea.

Your XML file will actually be a PHP file, in that you will have PHP code embedded in your XML code. For example, if you need some database stuff for some following bit of XML code, you'd write the appropriate PHP code just before to get all the data together and use PHP echo statements to put your database data into the XML tags.

xml_file.php:


<xml>
<?php
$db = mysql_connect(...);
mysql_select_db(...);

$result = mysql_query("select some_data from some_table");
while($row = mysql_fetch_row($result)) {
?>

<some_data>
<?php echo $row[0]; ?>
</some_data>

<?php
}//while
?>
</xml>


Does that sort of clear it up for you?

If anyone else has additional comments, I'm sure they will post them to help you out.

Sadiq.

firepages
03-10-2004, 02:34 PM
do I have to produce 10 xml files?



you can either do a simple cache of the XML file ..

<?
header( 'content-type:text/xml' ) ;
if(file_exists('blah.xml')){
readfile('blah.xml');
}else{
//generate output from DB and save as blah.xml
}
?>

then whenever the data in the database changes ,
delete blah.xml forcing a rewrite on the next page view...

OR (and personally I think the above is preferable ?)

<?
header( 'content-type:text/xml' ) ;
//build and output data
?>

weronpc
03-11-2004, 02:24 AM
To: Firepages


}else{

//generate output from DB and save as blah.xml

}


Do you think this will be fast? if I want search data, it will search in a text file.

To: Sad69
Your solution to me looks like only good for one user, what if 50 users visiting the site doing different queries? do I produce 50 xml files?

sad69
03-11-2004, 02:47 AM
heh, I guess I don't know enough about xml... so I'm sorry I can't answer that.

But in my mind I still think you only need the one file. But leave me outt a this one, I shouldn't have butted in!

Sadiq.

firepages
03-11-2004, 04:21 AM
searching an XML file when you have a database available seems pointless to me unless you want to search the XML file clientside ,
regardless you still end up with a blah.xml to work with.

so you could alter to...



assuming $var && $var1 are search parameters or identifiers
<?
$requested = "cache.$var.$var1.xml";
if(!file_exists($requested)){
//grab your content from the DB
//build your xml file
$xmlfile ='<'.'?xml'; $xml .='/*etc*/';
//etc .. & save it
$fp=fopen($requested,'w');
fputs($fp,$xml);
fclose($fp);
}
//output
header( 'content-type:text/xml' ) ;
readfile($requested);
?>


and yes you may end up with a cache of several XML files [perhaps named to suit the queryparameters ... cache.$var.$var1.xml where $var & $var1 are search parmeters] but there is nothing you can do about that , at least they only get created once the first time someone does that particular query (and again whenever the database content changes)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum