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


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 (shown in the snippet) and also available here: but got the same error

Here is the URL to this code on the server:

We have PHP 5.1.6 installed with XML functionality enabled:

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

Who is Participating?
hernst42Connect With a Mentor Commented:
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)
EdeneyeAuthor Commented:
Thank you. That was exactly it.
EdeneyeAuthor Commented:
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 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 to the site's module folder and edit that site's php.ini.

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

I restarted apache and everything worked as expected.

Thanks again!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.