Page 1 of 1

XML Data into HTML or PHP?

Posted: Thu Aug 13, 2009 4:29 am
by Reverend2009
Hi all, my first post...

I am trying to create an HTML version of my Flash website as easily as possible. My Flash website uses external XML for the content so ideally it would be great if I can use that same XML data in an SEO friendly HTML or PHP website. Obviously this means I have to keep the structure of the existing XML so that I can make edits to the XML and this would update both my Flash and HTML/PHP websites.

An example of the XML code (for my gallery):

Code: Select all

 
<?xml version="1.0" encoding="iso-8859-1"?>
<gallery> <img>
  <title>G1 Image One</title>
  <description>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus id lectus. Duis turpis dui,  id, rutrum sed.</description>
  <tmb>images/Master 135 x 91.jpg</tmb>
  <image>images/Master 824 x 554.jpg</image>
  </img><img>
  <title>G1 Image One</title>
  <description>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus id lectus. Duis turpis dui,  id, rutrum sed.</description>
  <tmb>images/Master 135 x 91.jpg</tmb>
  <image>images/Master 824 x 554.jpg</image>
  </img><img>
  <title>G1 Image One</title>
  <description>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus id lectus. Duis turpis dui,  id, rutrum sed.</description>
  <tmb>images/Master 135 x 91.jpg</tmb>
  <image>images/Master 824 x 554.jpg</image>
  </img> </gallery>
<title>YOUR GALLERY</title>
[/color]

And my HOME page:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<title>My Website</title>
<text>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sed mi vitae lacus ornare tristique. Fusce accumsan leo sagittis lectus. Aenean ornare erat malesuada ligula imperdiet imperdiet. Nunc iaculis faucibus quam. Maecenas porta dapibus ligula. Vestibulum mollis adipiscing lacus. Etiam pharetra elementum tortor. In hac habitasse platea dictumst. Curabitur turpis pede, mollis vel, fringilla ac, accumsan id, tortor. Maecenas porta dapibus ligula. Vestibulum mollis adipiscing lacus. Etiam pharetra elementum tortor. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Cras pretium placerat neque. </text>
<image>images/Master 824 x 554.jpg</image>
<logo>images/logo.png</logo>
[/color]

So, how can I achieve this? I have Googled the hell out of this but cannot seem to find a straightforward solution. I seem to remember seeing somewhere that I'd have to use JS and PHP to get the XML content displayed in a webpage, but it wasn't remotely clear how to actually do that...

Could anyone please help?

Re: XML Data into HTML or PHP?

Posted: Thu Aug 13, 2009 9:38 am
by izzy4505
There are several ways to do this. Purists would tell you to use XSLT, but since you're on a PHP forum, I vote we go that route.

First thing to do is learn some PHP if you haven't already. Give this a try...

Code: Select all

<?php
echo "Hello world";
?>
Read up on the SimpleXML class so you can access your XML data:
http://us2.php.net/simplexml
http://us2.php.net/manual/en/simplexml. ... -basic.php

From there, use PHP to generate your HTML page.

<?php
echo "<head><title>" . $mytitle . "</title></head>";
echo "<body>" . $bodystuff . "</body>";
?>

Stuff like that.

Re: XML Data into HTML or PHP?

Posted: Thu Aug 13, 2009 9:48 am
by Reverend2009
izzy4505 wrote:There are several ways to do this. Purists would tell you to use XSLT, but since you're on a PHP forum, I vote we go that route.

First thing to do is learn some PHP if you haven't already. Give this a try...

Read up on the SimpleXML class so you can access your XML data:
http://us2.php.net/simplexml
http://us2.php.net/manual/en/simplexml. ... -basic.php

From there, use PHP to generate your HTML page.

<?php
echo "<head><title>" . $mytitle . "</title></head>";
echo "<body>" . $bodystuff . "</body>";
?>

Stuff like that.
Thanks Izzy for the pointers! I have a couple of further questions...

1. I need to heavily style the resulting pages - as I say, these are to be HTML or PHP versions of my existing Flash site, which is... well, you know, flash! So I need to make sure the HTML website is equally impressive. Is this possible?
2. With that in mind, is there any reason not to just use PHP web pages? Or is it better to parse the PHP to HTML as you suggest?

Re: XML Data into HTML or PHP?

Posted: Thu Aug 13, 2009 10:04 am
by izzy4505
I think you're getting confused. PHP is only server side.

When you visit a site and you see .php on the file name extension, that isn't some magic code that your browser is running... what your browser got back was straight up HTML. (Technically it can be anything... even images! But PHP is typically used for creating web pages.) Think of PHP as instructions given to your web server to create a web page on the fly.

Code: Select all

<html>
<head>
<title><?php echo "This is my title!"; ?></title>
So, let's say a browser requests index.php from a web server. The server says, heyo this is a PHP file and hands it off to PHP itself. PHP goes through and takes whatever is in the file and sends it back to the webserver until it gets that magic bit of text saying <?php. At this point it starts executing code until it gets ?>. After execution, it sends that data back to the server, and then server sends it back to the browser. What gets sent to the browser is then....

Code: Select all

<html>
<head>
<title>This is my title!</title>
You should read up on PHP here:
http://us2.php.net/tut.php

Now, whether or not your page looks nice enough depends on your skills as a web designer and has absolutely nothing to do with the server-side technology you choose to generate your pages.