All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

It's just this damn XML

14 October 2003

I started working on a nice little personal project in PHP. It has a specific real-world use, and in evaluating the approaches I could take with the design, I decided that it didn’t need a database. Rare for me. I like databases. The only data this application needs to store is general system configuration settings, which I thought would be best stored as a simple XML file. I like XML – much more than databases.

So I began carrying out proof-of-concepts on each of the major parts of the project. I searched on the excellent php.net for the XML functions, and found them. Hmm.

Why is PHP’s XML implementation so crap? How has it managed this long without DOM support? I can see it’s on its way, but how has everyone been managing? Do PHP developers not care about XML?

I’ve settled with parsing my XML file out into an array of arrays (yuk), which is useable but hideous and putrid. I think I’m going to have to design my application in such a way as when DOMXML is widely supported in general PHP installations I’ll be able to easily recode to make use of it. At least there’s an interesting challenge in that.

Seriously, I’m not having a rant, I really want to know this. How do PHP developers deal with XML without a DOM? What do you use? Is there something altogether DOM-tastic out there that I’m missing?

I’ve had no problems with other parts of the project. The bit which I thought would be difficult (POP3 integration) was as easy as pie, if not easier. So praise be. It’s just this damn XML.

- Drew McLellan

Comments

  1. § Jacob Patton: I’ve only dabbled a bit with PHP’s XML tools, but I do remember a PEAR class that handled the job quite nicely—I was able to work up a news aggregator that did the job nicely until I got a Mac and NetNewsWire...

    I remember finding the sample scripts I used in O’Reilly’s PHP Cookbook. If you’d like, I can email you a couple of links/snippets.
  2. § Drew: Jacob - that’d be great.
  3. § vlad: i’ve always had to put up with event-based parsing. it’s crap, i agree.

    when i can, i use an xslt stylsheet (say to transform an xml file to xhtml or rss) but obviously for your situation this doesn’t help.

    i’d like to see a better way.
  4. § zlog: Having only dabbled with php’s xml functions I can say that I’m fairly impressed so far.

    What I’ve been playing with is almost exactly what vlad mentioned eg.

    php generates xml -> xml is parsed though an xsl stylesheet by php -> returned (valid) xhtml is styled with css

    What really gets me is php’s lame rendition of classes, but that’s another story.
  5. § Drew: I’m sure the xsl stuff is fine - you can’t go far wrong with xsl. I’ll be using some xsl, so I’ll see how it goes.

    As for classes .. PHP classes are great compared with what you get in VBScript.
  6. § george: I have to agree that, in general, PHP’s XML functions aren’t as good as they could be, but thats the beauty of open source, in that there a a lot of people working on improving them.

    I also think XML is over used. As a typical example, (although this doesnt apply to Drew, cos it’s perfect for your needs) I have seen developers, pull info out a DB into XML format and then parse the code and display it on the page, now thats an awful lot of work for no added benefit. XML is valuable, for example, when two machines need to talk to each other but otherwise couldn’t.
  7. § Dysfunksional Monkey: If you're only holding configuration information, why not use an .ini file?

    PHP has the function parse_ini_file(), which I find very useful. This pushes the information into an array, allowing for multi-dimensional arrays when using an ini syntax as follows:

    [database]
    server = localhost
    port =
    db = mydb
    un = username
    pw = password

    This, for me, its extreamily fast and easy to use. You can even push this to a session variable:

    $_SESSION['config'] = parse_ini_file('config.ini', TRUE);

    echo 'Hello {$_SESSION['config']['db']['un']}, you have now logged in to {$_SESSION['config']['db']['server']}.';

    All my projects are created in XML. I have an XML Database or file store. I use XSL for templates. I use PHP's XSL functions for translation before pushing to the browser. I use XUpdate for updating information within the DB. I use XPath to get it back out.

    I've never had a problem with it. But then again, I don't use PHP to mess with the XML. The backend and frontends I use don't need me to.

    I used to use PHP/MySQL. Wonderful as it is, I found that I would be parsing information, screwing with it, validating it, merging it, and generally passing it from pillar to post before anything really could be done with it.

    If you really want to use PHP for XML, try:
    http://phpxmlclasses.sourceforge.net/
    http://www.carrubbers.org/scripts/php/xpath/
    http://www.carrubbers.org/scripts/php/xmldb/
    These might help.

    Otherwise, look at:
    http://xml.apache.org/xindice/
    http://exist.sourceforge.net

    Happy coding!
  8. § Dysfunksional Monkey: Sorry, alteration to above code:

    if(!isset($_SESSION['config']){
    $_SESSION['config'] = parse_ini_file('config.ini', TRUE);
    }

    echo 'Hello {$_SESSION['config']['db']['un']}, you have now logged in to {$_SESSION['config']['db']['server']}.';

    Hindsight - the only place it works is with code. ;)
  9. § Isofarro: I used PHP as a webservice handler for an AtomAPI implementation. The only decent thing in PHP by default is the SAX processor, and you’re right it is seriously lacking.

    Have a look at http://www.phpclasses.net/ - sign up as a member. Their XML section contains a number of useful routines to handle, produce and manipulate XML.
  10. § Massimo Foti: Yes, PHP’s XML implementation is very crude. It only offer a SAX parser, acutally one that isn’t developed anymore...

    The DOM libraries looks promising, but they aren’t ready from prime time yet (it has been this way for years).

    The XSLT libraries aren’t bad but, like the DOM libs, they aren’t installed by default, so often they aren’t available on shared servers...

    Sure, there are good libraries that abstract XML parsing, SOAP, XML-RPC and the like (using SAX in the background), both from PEAR and other sources. But I firmly believe this kind of stuff should be available out of the box

Photographs

Work With Me

edgeofmyseat.com logo

At edgeofmyseat.com we build custom content management systems, ecommerce solutions and develop web apps.

Recent Links

Affiliation

  • Web Standards Project
  • Britpack
  • 24 ways

About Drew McLellan

Photo of Drew McLellan

Drew McLellan has been hacking on the web since around 1996 following an unfortunate incident with a margarine tub. Since then he’s spread himself between both front- and back-end development projects, and now is Director and Senior Web Developer at edgeofmyseat.com in Maidenhead, UK (GEO: 51.5217, -0.7177). Prior to this, Drew was a Web Developer for Yahoo!, and before that primarily worked as a technical lead within design and branding agencies for clients such as Nissan, Goodyear Dunlop, Siemens/Bosch, Cadburys, ICI Dulux and Virgin.net. Somewhere along the way, Drew managed to get himself embroiled with Dreamweaver and was made an early Macromedia Evangelist for that product. This lead to book deals, public appearances, fame, glory, and his eventual downfall.

Picking himself up again, Drew is now a strong advocate for best practises, and stood as Group Lead for The Web Standards Project 2006-08. He has had articles published by A List Apart, Adobe, and O’Reilly Media’s XML.com, mostly due to mistaken identity. Drew is a proponent of the lower-case semantic web, and is currently expending energies in the direction of the microformats movement, with particular interests in making parsers an off-the-shelf commodity and developing simple UI conventions. He writes here at all in the head and, with a little help from his friends, at 24 ways.