Ben Ward

PHP4 & XML... Help please

.

I spend a vast amount of my time programming. I do it for most of my day at work and I do a lot at home too (‘cause I’m a bit of a nerd, really).

At work, we use Microsoft’s very powerful .NET tools to code Windows and Web applications. One thing they do rather well is XML handling. You can do rather a lot with it.

PHP4, one of the languages which I have access to on this site, doesn’t seem to do XML anything like as well. This is a bit-of-a-pain-really™. In fact, it’s very frustrating because I’d very much like to have a generic XML parser at my disposal so I can parse in some OPML feeds of my blogroll rather than the (seemingly unreliable) RSS I get at the moment. And to do it myself would be nice, y’know?

So, this is a bit of a plea: Can anyone point me towards “how to use XML in an intuitive manner in PHP4”?

I’ve a couple of ideas that I don’t know the possibilities of:

  1. We also have Perl installed on this server deal of ours. I have been known to do a bit of Perl in my time, has anyone got some experience of using Perl from within a PHP script to generate something XHTML, from the other XML format. I could run the Perl process via Cron and generate XHTML for the PHP to pick up later… maybe that would work well.

  2. Maybe the most elegant way… can PHP do any kind of server side XSL Transformation of an RSS, OPML or whatever feed into XHTML? I could then output the coverted XHTML. Again, I’ve never done any XSLT so I have no idea what’s involved here.

Thanks.

Comments

Previously, I hosted responses and commentary from readers directly on this site, but have decided not to any more. All previous comments and pingbacks are included here, but to post further responses, please refer me to a post on your own blog or other network. See instructions and recommendations of ways to do this.

  1. What fun is .NET when everything is done for you already? My friend and I have the friendly rivalry going. He’s a .NET guy, I’m a PHP guy.

    In any case, I wrote a php OPML renderer. It’s certainly not a generic XML parser but it could be extended to be. What it will do is parse OPML files.
    http://www.mikemontalvo.com/index.php?blog=6
    If you are interested in the code, shoot me an email.

  2. Ben

    Personally I don’t compare PHP and .NET very much. I work in both – .NET on some really rather huge projects at work and PHP at home where freeness is a virtue. I think that both languages are capable providers of fun, the scale of project tends to be different though.

    In any case, I don’t find having to handle open-tag and close-tag parsing fun. I think it’s tedious and requires far more effort than it really should, especially when dealing with a data format as vital as XML.

    What I did in this situation (the night before you linked to your parser, which looks cool, by the way), was to implement a very, very basic XmlDocument object in PHP. It contains a tree of XmlNode objects (with attributes stored in a simple associative array within an XmlNode). It also implements a getElementsByTagName() method. I will admit, proudly, that I had a lot of fun doing it too. So long as the XML is valid, it works really nicely, but I need more error handling for invalid ones.

    I’ll hopefully release it soon, but I need to comment it better first.

    On related a note: Sadly I discovered that another problem with my Blogroll is that often I don’t get a complete download of the RSS feed from blo.gs… That seems harder to fix and maybe a task I should ship out to a separate Perl process.

You can file issues or provide corrections: View Source on Github. Contributor credits.