True Seperation of Presentation & Content with  XML

Web standards praises the idea of separating presentation from content. Unfortunately, doing so is harder said than done in the professional world.

When I begin working on a redesign I usually find myself spending countless hours re-formatting content, so what’s the point? If a client wants specific information laid out as an unordered list (or outline) one day and tabular data the next, you’ll find yourself rebuilding the page’s structure.

The best solution to this ongoing probably is to utilize XML to describe the content as best as possible. The one problem with this approach is that parsing that XML into usable code is typically difficult. With PHP5’s implementation of the SimpleXML object parsing XML is finally as simple as it should be.

This is the best way to handle web content in my opinion. The advantages as I see them are:

  • Store your content in one central location, even it is used in multiple places throughout a site
  • Write your content once using the most semantic markup of all, XML
  • Loop through tags to make styling repeated content a breeze (e.g. Press Releases page)
  • Use your content with different technologieslore
  • Create RSS feeds, supply it to other domains

Using the SimpleXML object

Let’s get our hands dirty and see how to parse an XML file with SimpleXML.


<?xml version="1.0" encoding="utf-8" ?>
    <date>August 26</date>
    <name>New press release!</name>
    <description>Press release summary or explanation would be here, if this wasn't a demo.</description>


$press = simpleXML_load_file("press.xml"); 
foreach ( $press as $articles ) { 
<div class="article">
  <h2><a href="<?= $articles->link ?>"><?= $articles->title ?></a></h2>
  <p><?= $articles->description ?></p>
  <p class="date"><strong>Published</strong> <?= $articles->date ?>, <?= $articles->year ?></p>

XHTML (generated by SimpleXML)

<div class="article">
  <h2>New press release!</h2>
  <p>Press release summary or explanation would be here, if this wasn't a demo.</p>
  <p class="date"><strong>Published</strong> August 26, 2008</p>

Download source

Download the SimpleXML Source Code for the example above.

I was introduced to the SimpleXML object while browsing PHP 5 Recipes: A Problem-Solution Approachir?t=kn08 20&l=as2&o=1&a=1590595092. I would recommend this book to professional developers looking for a great desk reference resource. As best put by the author:

“This book is a source of instant solutions, including countless pieces of useful code that you can just copy and paste into your own applications, giving you answers fast and saving you hours of coding time.”

That said, it’s big, long, and definitely not something you sit down and actually read. It provides a well documented reference of PHP 5 that includes some newer additions such as SimpleXML.


Meet the Author

Kevin Leary, WordPress Consultant

I'm a custom WordPress web developer and analytics consultant in Boston, MA with 16 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.