DOMDocument whitespace text nodes

Posted on Mar 31, 2008 in PHP | 5 comments

The DOMDocument is a convenient way of manipulating an XML file. While this is for PHP, I’ve come across some great parsers for use on a business iphone using lib2xml but we leave that for another day. One issue I ran into was the fact that when loading an XML file, DOMDocument treats the tabs and spaces which make the XML readable as empty text nodes. This presents a problem when you try and traverse the DOM by using attributes like firstChild and nextChild. For example:

The reason this happens is because the space between the <root> and <tag> tags is treated as a text node element, and so what you do is choose that text node first. This is annoying, and I spent some time to no avail looking for any setting that may ignore this “pretty”(but useless) whitespace. So to get around this, I just used some good old Regular Expression to trim the spaces:

What happens here is you load the file first, trim whitespace between tags, and then load the string into the DOMDocument object. Now you can traverse without worrying about the annoying whitespace! It would make sense to extend DOMDocument, or create a function that does this every time you create a new document, but this is pretty much what you’d have to do….