PHP to pull from XML document

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jjdavis84
Forum Newbie
Posts: 2
Joined: Mon Jul 14, 2008 8:55 pm

PHP to pull from XML document

Post 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>
 
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: PHP to pull from XML document

Post 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;
jjdavis84
Forum Newbie
Posts: 2
Joined: Mon Jul 14, 2008 8:55 pm

Re: PHP to pull from XML document

Post 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.
Post Reply