Web Programming

A working Microformats extension to SimpleXML

Oliver Brown
— This upcoming video may not be available to view yet.

I’ve completed a basic version of a class that (sort of) extends SimpleXML. When I say sort of I mean it extends a wrapper class (ExtendXML) that inlcludes all the functionality of SimpleXML.

Download the following to try it out:

The object is created the same way as ExtendXML. After creating it you must also call:

// $mf is a MicroformatXML object 
$mf->mf_init();` 

This adds a new property, $mfHCards, which is an array of hCards. You can access the various hCard values as properties of these objects. As an example:

$mf = MicroformatXML::create\_from\_file($xmlFile);
$mf->mf\_init();
foreach ($mf->mfHCards as $hCard) {
    print $hCard->fn .'  ';
}

Please note this is a very early version that is undocumented and largely untested. It also doesn’t contain every hCard property.

Extending SimpleXML

Oliver Brown
— This upcoming video may not be available to view yet.

I posted a while ago about problems with extending SimpleXML (which I intend to do so I can add automatic microformat support). Well I think I’ve come up with a solution.

I’ve created a class called ExtendXML that is completely extendable (see the other post for an explanation of why SimpleXML isn’t extendable) that provides all of the functionality of SimpleXML.

The initial call is similar to using SimpleXML (except I made the functions static class methods instead):

ExtendXML::load_string($xmlString, [$class, [$simpleClass]]);` `ExtendXML::load_file($xmlFile, [$class, [$simpleClass]]);`

The $class argument contains the name of the class that will be returned. This should extend ExtendXML and defaults to ExtendXML.

The $simpleClass argument contains the name of the SimpleXML class that will be passed to SimpleXML functions. This should extend SimpleXMLElement and defaults to SimpleXMLElement.

The object returned can then be used the same as a SimpleXMLElement object.

I haven’t tested it much or documented it yet, but it supports child tags (returning objects are of the class ExtendXML or whatever you specified), attributes and xpath expressions.

ExtendXML

PHP5, OOP, ArrayAccess, SPL, programming, code, tutorial, microformats, XML, SimpleXML

Issues with extending SimpleXML

Oliver Brown
— This upcoming video may not be available to view yet.

I wrote a post about my attempts to extend SimpleXML to support microformats. Well it’s not as it easy as it seems.

You can’t define new properties of a class extending SimpleXMLElement. Well you can define them but they don’t work - when you access them they always return a SimpleXMLElement object (or actually an object of whatever class you defined).

I’ve tried overriding __get and __set and neither work so the only thing I can think of is to create a new class that delegates most of it’s work to SimpleXMLElement.

Microformats extension to SimpleXML

Oliver Brown
— This upcoming video may not be available to view yet.

I’m writing a PHP5 object that extends SimpleXML to automatically add various microformats. So you could do, for instance, the following:

$xml = SimpleXML\_load\_file('test.xml');
foreach ($xml->hEvent as $event) {
    print $event->original();
}

This would print the original HTML of every hEvent found in the HTML document. XML, PHP, PHP5, microformats, hCard, hReview, hCalendar

Code generation in PHP

Oliver Brown
— This upcoming video may not be available to view yet.

I’ve started working on a class-based code generation utility for PHP (in PHP). Like the scripting engine it’s working out simpler than I expected. Quite how powerful or indeed useful it could be is yet to be seen. The way it works is by allowing you to define fragments of classes. For example the Singleton fragment is:


class !className {
    static private $instance;

    private function \_\_construct() {
        // Code Start
        // Code End
    }

    static public function Init() {
        if (!self::$instance) {
            self::$instance = new !className();
        }
        return self::$instance;
    }
}

You create a class with some number of fragments. Each successive fragment adds specified method and properties to the class. If two (or more) fragments define the same method (probably common with constructors for instance) the the code for the second fragment get’s added in the //Code Start ... //Code End sections. Ultimately you will be able to change your model (defined in XML and it will update your code accordingly, maintaining all the code within the //Code Start ... //Code End sections.

DOM foolery

Oliver Brown
— This upcoming video may not be available to view yet.

Since I’ve been making random DOM and JavaScript posts recently I thought I’d share a clever bog post I found cleverly called DOM-foolery. It describes a relatively simple way to make images in web pages have curved corners.

How about Cash City Wars

Oliver Brown
— This upcoming video may not be available to view yet.

That’s the name I’ve come up with for the game since it’s vaguley based on Cash Wars. Or at least will be. I’ve started adding descriptions to towns. There are a few issues still though, most notably the existence of multiple towns with the same name which I am eliminating (I’m only intending to have the largest 100 towns).

Cash City Wars

Finally a use for Netscape!

— This upcoming video may not be available to view yet.

I’m a Mac user (and proud). When it came to the ‘big switch’ there were several things that I needed: my documents, my music, my pictures and my internet (emails and bookmarks and so on). All of these were easily copied across using a cross over cable, no problems there - but shock horror, my emails were another story. Microsoft Outlook Express stores emails in a non-standard format which can’t be read by Apple Mail - disaster! It was then that I remembered once installing Netscape to test compatibility of my web site. Netscape had very nicely offered to import the Internet and email settings from Internet Explorer and Outlook Express. Checking up on this I found that when it does this it stores the resulting emails in a standard .mbox format - which can then be read by any sensible email client. Wohoo! So the basic steps to import your emails from Outlook Express on Windows to Apple Mail on the Mac:

  • Install Netscape on the Windows box.
  • Allow it to import from your Outlook Express.
  • Copy the newly created .mbox files to your Mac.
  • Show Apple Mail where it can find them when it asks.

That’s it - yay! Ooo and make sure you remember to uninstall Netscape afterwards or your PC might become contaminated or something hehehe.

Start semantics simply

Oliver Brown
— This upcoming video may not be available to view yet.

You may or may not have heard of microformats. They’re basically along the lines of the semantic web project but in a more practical way that is more likely to take off. It basically involves adding extra bits of semantics to web pages (or any XML-based documents) in a consistent way that doesn’t limit what already exists. But there are much simpler good practices that need to be encouraged regarding semantic markup first.

This blog post illustrates a wonderful misuse of HTML and offers an important explanation about why it is a misuse (or at least a decidely-less-useful use).

Bad DOM

Oliver Brown
— This upcoming video may not be available to view yet.

The Dom Scripting Taskforce says adding non-standard markup to page using Javascript is OK. Well I wouldn’t call it a “best practice” but from a personal point of view I can’t see a practical disadvantage.

For me the problems with bad markup come from trying to parse it automatically (using PHP’s SimpleXML object for instance). Since the javascript will basically only affect the page a human sees and not a machine, many of the disadvantages of non-standard markup fade away.