Page 1 of 1

PHP to pull from XML document

Posted: Tue Jul 15, 2008 12:45 am
by jjdavis84
Does anyone have any suggestion on how to modify the following so it will pull the file names from an XML file so I can update the XML file instead of the PHP file? Hopefully that makes sense.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<?php
 
$files = array(
    0 => 'test1.php',
    1 => 'test2.php',
    2 => 'test3.php'
);
$index = array_rand($files);
include($files[$index]);
?>
 
<body>
</body>
</html>
 

Re: PHP to pull from XML document

Posted: Tue Jul 15, 2008 2:06 am
by alex.barylski
Well if you have SimpleXML available that is what it is ideally suited for, reading XML files.

Code: Select all

$xml = simplexml_load_file ('somefile.xml'); // Make sure it valid XML file or I think parser fails
Then you access elements like this:

Code: Select all

echo $xml->file1;

Re: PHP to pull from XML document

Posted: Tue Jul 15, 2008 3:17 am
by jjdavis84
trying to learn as I go... what would the XML file be like? Could you explain it a little more... Thank you so much for your help.