Other articles
- Center a web page in CSS »
- Basics of Database optimisation »
- How to configure Outlook Express to receive emails »
- How to configure Outlook 2007 to receive emails »
- HTML Base Tag »
- Scaleable HTML and CSS Guide, Part 1 - Introduction »
- Problems Encountered With PHP DOM Functions »
- Refactoring code with find, xargs and sed »
- What do webdesigners need from clients »
- Using any font on a website »
Recently we have been making heavy use of PHP's excellent DOM functions, which hugely improve on the DOM XML functions from PHP 4. The DOM functions have been used for two purposes:
- As part of a PHP framework, we have been building a XSLT based templating engine, which will replace our much slower and less flexible regular expression based engine.
- For a variety of SERP crawling SEO tools, including Rank Check, a tool allowing you to check your search engine rankings in a number of countries.
Naturally, whenever exploring any new programming avenues you are going to come up against a few brick walls. Due to the sparse documentation of the DOM functions we have documented a few problems that cropped up.
Problems Parsing Invalid HTML or XML
When loading invalid HTML or XML into your DOM object, you will be presented with PHP warnings. If you are scraping web pages, the chances are you will get a lot of warnings similar to: Warning: DOMDocument::loadHTML() [function.DOMDocument-loadHTML]: htmlParseEntityRef: no name in Entity, line: 32 in C:\www\md_framework\rankings.php on line 174. Although these warning will not affect the process of your script, they will obscure any data your script may output. To combat this problem, simply use the error controller operator (@) as follows:
DOMDocument->getElementById Can't Find IDs in XML
One of the most essential functions in DOM manipulation that will be familiar to all JavaScript gurus in the getElementById function. It may come as a surprise to anyone using the DOMDocument->getElementById method on XML documents - that it doesn't work. There are three solutions for this problem:
- Use the
DOMElement->setIdAttributefunction on the DOM element in question. This will now allow you to use theDOMDocument->getElementByIdmethod on the selected XML element - Set 'id' as an attribute in your XML's DTD
- Use XPath to find the attribute.
First Argument is Expected to be a Valid Callback
This one has nothing to do with the DOM, but I thought I'd throw it in anyway as it came up at the same time. You will probably only encounter this error when you are creating a beastly class, as it involves a member of the Function Handling Functions.
When using the call_user_func_array function to call a method of an object you may get the error First argument is expected to be a valid callback. This will happen if the method you are calling is set to private. Usually you would expect to get an error stating you are trying to access a private member of an object, but because you are using the call_user_func_array function to call a method you get the invalid callback message. You get this error because call_user_func_array is not allowed to call private methods of a function.







Comments
Danp129
15th October, 2009 at 8:59am
thanks a lot!