A DOM Experience

June 9, 2010

I’m currently writing a website, and trying out different things to learn what the best solution is. This time around I was playing around with how to easiest create the HTML elements and attributes, and ended up with creating a simple HTML class. When I looked at it, it reminded me of DOM – and I asked myself – why didn’t I use that in the first place?

I use mainly DOM to create XML, and since XHTML has a XML structure and can be validated with a DTD – It should work well to build it with DOM elements and attributes.

In the beginning it seemed harmless and went pretty well, but when I then wanted to test my code it went bad. The problem was the same thing that I struggled some years back when I first was introduced to DOM, and I think it may be the one reason that it makes hard to program DOM in an object-oriented fashion. DOM is not a very flexible way to create a whole advanced web page in anyways, but maybe especially if you have chunks of HTML returned by objects.

The problem of DOM in my opinion is simply that you cannot just create an element and add it to a tree. No, you need a DOMDocument object, to create the element belonging to the tree, and then you have to add the element to an element already on that tree. Why is this so bad then? Because you cannot make chunks of XML and add it in to the document without having access to the parent and the root element. It can become pretty messy to pass them along the whole time.

DOM is great for modifying HTML and XML, getting stuff out and adding content to the tree – not creating them. That is my current opinion. Maybe there is some good ways to do it, but I didn’t want to waste my time anymore experimenting on it.

Back to my HTML class: This one can do it just as I created it to, generating HTML – not modifying, not adding content to a file nor getting stuff out.

Advertisements