Using importNode and appendChild with PHP 5 DOM

importNode is one of the DOM functions in PHP 5 that I struggled with for a while. What I wanted to do was to take an XML node from one document and insert it into another, and somehow that wasn’t particularly easy. I tried using ‘appendChild’ but kept getting ‘wrong document’ error messages. Once it was working it seemed obvious, but the example in the documentation wasn’t entirely clear to me.

As an outline, the steps that need to be gone through are:

  1. Import the node you want into the destination document. This is done by calling the importNode method and storing the result in a variable (the crucial step). At this stage, the node is in the document, but won’t appear anywhere if you print it out which may seem odd.
  2. Append the stored node to the destination document in the place you want it

The thing that threw me is that the importNode method doesn’t so much import the node as make a copy of it in the destination document – so the original is actually left untouched. This seems to be standard across XML DOM methods in other languages such as C# and JavaScript.

The code then is as follows. This is for taking a complete document and moving it into a new DOMDocument object. The existing xml is assumed to be loaded into $oldXML

$xml = new DOMDocument();
$xmlContent = $xml->importNode($oldXML->documentElement,true);
$xml->appendChild($xmlContent);

It is important to use the second parameter ‘true’ in importNode as this tells the method to import all children as well as the selected node. The node that $xmlContent is appended to can be any DOMElement. Note that importNode is a method of the DOMDocument (and must always be) as it is the document as a whole that the new node is being imported into, not a specific node.

importNode in the PHP 5 DOM documentation
appendChild in the PHP 5 DOM documentation

Using removeChild with the PHP 5 DOM

The documentation for PHP 5’s DOM functions isn’t at its most helpful yet, so I thought an example of how to use ‘removeChild’ wouldn’t go amiss.

Assuming first that you have some DOMDocument XML in a variable called $xml, that may look something like this:


My title

The first thing to do is to get a handle on the node you want to remove. We’re going to remove the node named ‘title’:

$node=$xml->getElementsByTagName("title")->item(0);

Then, simply remove $node from its parent:

$xml->getElementsByTagName("labels")->item(0)->removeChild($node);

Not too much to it at all.

PHP DOM removeChild documentation

Review of the Honda CBR 1100 Super Blackbird

I’ve just had the pleasure of a week and a half with one of Honda’s biggest, and fastest, bikes: the Super Blackbird. Thankfully, no-one calls it the ‘Super Blackbird’ but just sticks to the plain and simple ‘Blackbird’.

Before getting on the bike I was slightly unsure about whether I’d take to it. I loved the CBR600, didn’t get much out of the Deauville, and didn’t take to the VFR800 because of its weight, handling andgeneral feel. The Blackbird, with its 1100cc engine, has the weight of the second two and more power and speed than any of them.

The first thing about the Blackbird to notice is the weight; more precisely, the fact that it doesn’t feel like a heavy bike. I can only think that the centre of gravity is low as it feels barely heavier than the CBR 600 and certainly lighter than the VFR. The bike’s length is also less noticeable than I would have guessed, although inevitably the riding position is further back than the 600 if a similar sporty-but-not-too-uncomfortable forward-leaning style.

What you can’t help but notice, though, is the power. Hit the open road and open the throttle and you realise you’re sitting on a rocket with wheels. It is very smooth and even in delivery, although accelleration peaks at around 7000rpm and continues well up to around 10000rpm, but the big engine pulls well from any revs in any gear. This continues well past 100mph, and it’s easy to reach 125mph in 3rd gear in just a few seconds. In fact, the bike will still leap forward in 4th gear at a little over 100.

Once the bike’s moving the size isn’t noticeable. Even round twisting country roads this feels pretty close to a nimble sports bike. It’s only at walking speed pace that you feel the length (or,as often happened, when you speed past a turning and have to make a u-turn somewhere). The front wheel feels pretty nailed to the road, although maybe not quite so much as the CBR 600 but definitely better than the VFR 800. The acceleration means that you need never get stuck behind a caravan or a sunday driver as only the smallest gaps are needed to power past any car, and should something come round the corner then there’s always some more power in reserve.

Where the bike really shows its breadth of appeal, though, is over long distances and with a pillion passenger and luggage. With two up the weight of the bike turns into an advantage as you hardly notice there’s someone on the back, and the 1100cc engine on the Blackbird means there’s not much lacking in the power department either. The single-piece seat and grab rail are also comfortable for your passenger which is rare on such a high performance bike. The exhaust pipes are low enough to throw some panniers over, and there’s even hard luggage available.

The faring is slightly higher and broader than you generally find with sports bikes and helps keep the wind off on long motorway journeys. Nothing can really help these but I wouldn’t want to try a 4 hour ride on anything less now.

In summary, the Honda Blackbird is a big bike that feels much smaller than it looks, and the power from the 1100cc engine and good faring means adapting to medium distance touring is also very easy.