We help IT Professionals succeed at work.

Cannot use DOMDocument class for PHP even though everything seems to be installed on server

Edeneye asked

I am reading and going through the examples in the book "Beginning Ajax" by Wrox.

Pretty quickly I ran into trouble. I was unable to use the DOMDocument class in PHP.

The error I am getting is "Fatal error: Class 'DOMDocument' not found in /var/www/html/dev/ch3/test.php"

After looking around the web, I found that usually it was because the PHP-XML was not installed on the server, so I installed that. Still no go.

I decided to try a more basic example from PHP.net (shown in the snippet) and also available here: http://us2.php.net/manual/en/function.dom-domdocument-savexml.php but got the same error

Here is the URL to this code on the server:http://www.mvtimes.com/dev/ch3/test.php

We have PHP 5.1.6 installed with XML functionality enabled: http://www.mvtimes.com/info.php

Any insight or help would be greatly appreciated.

Thank you,
$doc = new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput = true;
$root = $doc->createElement('book');
$root = $doc->appendChild($root);
$title = $doc->createElement('title');
$title = $root->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
echo "Saving all the document:\n";
echo $doc->saveXML() . "\n";
echo "Saving only the title part:\n";
echo $doc->saveXML($title);

Open in new window

Watch Question

Top Expert 2007
You have compiled dom, but not loaded it into php. In your info you must see something like:

DOM/XML       enabled
DOM/XML API Version       20031129
libxml Version       2.6.27
HTML Support       enabled
XPath Support       enabled
XPointer Support       enabled
Schema Support       enabled
RelaxNG Support       enabled

Edit your php.ini and add a line like:
(look in /usr/lib/php/modules for the correct name)


Thank you. That was exactly it.


hernst42 got it exactly right, thanks.

For others that may come across this, the site I am working on is a virtual site on the server.
The dom.so extension was in the server level /usr/lib/php/modules but not in the site's correspondin folder.

I didn't really want to fiddle with it too much, so I assumed that since I had a separate modules folder for the site and a separate php.ini for the site that I should copy the dom.so to the site's module folder and edit that site's php.ini.

So I copied /usr/lib/php/modules/dom.so to /home/virtual/site1/fst/usr/lib/php/modules and added the line
extension=dom.so to /home/virtual/site1/fst/etc/php.ini

I restarted apache and everything worked as expected.

Thanks again!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.