My guess would be that you should try documentElement instead of firstChild on the domDocument. (And perhaps it would be easier to stick with the firstChildElement and nextSiblingElement functions, instead of working with DomNodes the whole time...)
(This is a shot from the hip...no guarantees on correctness ;-)
Bookmarks