TheDrunkenEpic - Drunken Ramblings of a Code Monkey
« Back to blog

DomQuery Alpha Released!

Well, I did some cleaning up of the code and appropriately commented everything. I'll be adding this to a Google Code repository very soon and get a wiki going. The documentation is pretty poor, but there are inline comments to help you for the interim; this project is only a few days old, mind you.

I added another method I mentioned in the previous DomQuery article. There was an issue with implementing a jQuery-like 'each()' method. I wanted users to be able to pass a runtime-generated callback function as an argument to be applied to all members of an xpath result. I found the solution to this problem by using PHP's 'create_function' function.

So, you can now do stuff like the following.

The Xml:
<root>
    <item>Item One</item>
    <item>Item Two</item>
    <items>
        <subitem>Sub-Item One</subitem>
        <subitem>Sub-Item Two</subitem>
        <subitem>Sub-Item Three</subitem>
    </items>
    <item>Item Four</item>
    <item>Item Five</item>
</root>
The Code:
$Xml = new DomQuery;

$lambda = 'echo $context['element']->nodeValue;'
        . 'echo ' ' . $param1 . $param2 . '<br/>';';

$Xml->load($xml)
    ->path('//item/*')
    ->each($lambda, 'foo', 'bar');

header('Content-Type: text/xml');

die($Xml->saveXml());
The Result:
Item One foo bar
Item Two foo bar
Item Four foo bar
Item Five foo bar

The 'walk' method is still quite useful as you can apply a previously defined function or class method to each member of an xpath result. This allows you to create plugins on-the-fly that allow you to manipulate your DOM in anyway you please!

Again, I'm always open to suggestions and critisms. This is a work-in-progress, so please keep that in mind! I'll be adding massive amounts of documentation and examples as I have time.

Download: Latest Version

Loading mentions Retweet

Comments (0)

Leave a comment...

 
To leave a comment on this posterous, please login by clicking one of the following.
Posterous-login     twitter