February Meeting – XML_Tree/RSS

When people ask me for a list of “why PHP” I often include in my response that it has All of the Buzzwords. Simply put, if there is a reasonable web-related standard out there, chances are that there is class or something to work with it in PHP. XML may or may not be a buzzword, but whithout question PHP does support it.

Learn more about working with RSS and other XML types in this month’s PUG! Read on for more

The problem may be that PHP supports XML processing in a bunch of different ways but only one (or maybe two) of them ship standard. This can make it tough when you don’t run your own server

Also, I won’t be covering schema’s or DTDs, you can find info on those at the w3c website. What I do want to show are ways to work with XML that don’t require any additional compiling or loads of work!

As usual, when I want to work on something new,I look at what is in pear first. There are a ton of places for other classes out there (like phpclasses.org) but there is also (quite often) a whole lot of junk out there too

PHP ships with the expat libraries enabled these days for parsing XML. While it works… well, let’s just take a trip to the onlne doc, shall we…

Notice that it is event (or call-back) based. That’s all well and good…and see how easy it is to manipulate start tags into something and end tags into something….Hmmm, isn’t that what XSLT is for? Anyway, I’m not even going to cover this. I’ve used it…it’s in a production system, actually…but I had to keep track of state in class variables, etc…it was not pretty. Maybe there are better ways to do this, but it does take practice to wrap your arms around it. If it works for you, go for it…it is the fastest and most efficient way to parse XML.

One of the many who know what they are doing, has written an RSS parser using the expat extensions. Perhaps more accuratly, using the Pear XML_Parser which helps you write event parsers in a class.

RSS – A very popular XML application

As I understand it, RSS is a small part of the Resource Description Framework (RDF). Put simply, it is a way for sites to publish meta-data about their content for other applications (and sites) to use. While this has not cough on with many of the big sites, for fear of loss of revenue (cough, cough) or whatever…there are plenty of RSS feeds out there on the web.

First, let’s look at a simple RSS feed in the docs for XML_RSS.

Let’s start actually working through some other examples:

  • Here is a goofy little function to (poorly) draw out an html representation of the feed. Source: rss.php
  • And a little program to drive it. Source:simple.php
  • And finally, what we get when we run simple.php
  • What about other types of XML?

    Of course, working with a canned standard and a canned class is one thing, but what if we need to work with our own xml layouts?

    Just for the sake of example, we are going to write our own XML application. That’s application in the XML sense of the word. We are going to make up our own file format to hold the “feeds” array.

    Of course, we will take it a bit farther though, as XML is just so darn flexible

    So, let’s start with our Example XML file.

    First off, notice that we have kept the concept of “feeds”. These are just “groups” of RSS links. Notice, that we have even sprinkled some formatting data into our XML. This is generally BAD, but I’m just trying to give examples.

    Now, we are going to keep the same drawFeed() function that we used before…but we do want to try to swank it up a little, right? Also, we are going to use the pear library XML_Tree. This was written with the intention of creating an object-oriented XML writer for php. However, the author added the ability to read in an existing document and viola.

  • Take a look at the new code: Source: better.php
  • As well as the Output of better.php.
  • The new formatting (aka pretty stuff) is all handled by a few lines in the style.css.
  • For an easy listing of the demo files, just go to the doghouse

    Keep in mind, the XML_Tree library can be used for outputting XML in a clean (sorta) OO kind of way. Check out the docs for XML_Tree for more details.

    Happy PHPing!
    Dan Holmes

    2 thoughts on “February Meeting – XML_Tree/RSS

    1. February Meeting

      Now I find this too. Looks like a HowTo on writing your own RDF/RSS parser using the SAX style parser that comes with php. I have just skimmed it, as I would much prefer to use the “standard” class that we used. But it might be a helpful resource, never the less.

      Plugging RDF Content into your website with PHP

      Don’t forget, you can use the printer friendly version if you don’t want to read one paragraph at a time. 😉

    Leave a Reply to Daniel Holmes Cancel reply

    Your email address will not be published. Required fields are marked *