When working with XML processing in PHP, the DOM extension is essential. Discovering that your PHP was compiled with --disable-dom
can be frustrating, especially when you need its functionality immediately and recompiling PHP isn't an option.
On Fedora/RHEL-based systems, PHP extensions are typically available as separate packages. Instead of recompiling PHP, you can install the DOM extension through yum/dnf:
sudo dnf install php-dom
# Or for specific PHP versions:
sudo dnf install php7.4-dom
sudo dnf install php8.0-dom
After installation, create a test script to verify DOM functionality:
<?php
$dom = new DOMDocument();
$dom->loadXML('<root><test>DOM is working!</test></root>');
echo $dom->getElementsByTagName('test')->item(0)->nodeValue;
?>
If the package isn't available in your repositories, you can compile just the DOM extension:
sudo dnf install php-devel libxml2-devel
cd /usr/src/php/ext/dom
phpize
./configure
make
sudo make install
Then add extension=dom.so
to your php.ini file.
If you encounter problems:
- Check
php -m
to see if the extension is loaded - Verify the extension_dir in php.ini matches where dom.so was installed
- Ensure all dependencies (like libxml2) are properly installed
The DOM extension can be memory-intensive for large XML documents. For better performance with huge files, consider combining it with XMLReader:
<?php
$reader = new XMLReader();
$reader->open('large_file.xml');
while ($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'item') {
$node = $reader->expand(new DOMDocument());
// Process the node
}
}
?>
When working with PHP on Fedora 10, you might notice that the DOM extension is disabled by default in the default PHP installation. This becomes apparent when checking phpinfo()
output and seeing --disable-dom
in the configure command. The DOM extension is crucial for XML processing, and many modern PHP applications require it.
While recompiling PHP with --enable-dom
is technically possible, it's often impractical because:
- It requires development tools and dependencies
- Might break system package management
- Could cause conflicts with future updates
- Time-consuming compared to simpler solutions
On Fedora and other RPM-based systems, the better approach is to install the DOM extension as a separate package:
sudo yum install php-xml
This package typically includes not just DOM but also other XML-related extensions like SimpleXML and XMLReader.
After installation, create a test script:
<?php
if (extension_loaded('dom')) {
echo "DOM extension is loaded!";
} else {
echo "DOM extension is NOT loaded!";
}
?>
You should also restart your web server for the changes to take effect:
sudo service httpd restart
If the package isn't available in your repositories, you can try compiling just the extension:
sudo yum install php-devel libxml2-devel
pecl download dom
tar xzf dom-*.tgz
cd dom-*
phpize
./configure
make
sudo make install
Then add the following line to your php.ini:
extension=dom.so
If you encounter problems:
- Check
/var/log/httpd/error_log
for PHP-related errors - Verify the extension directory with
php -i | grep extension_dir
- Ensure the DOM shared object file exists in the extension directory
- Check for multiple php.ini files with
php --ini
While the DOM extension is powerful, it can be memory-intensive for large XML documents. For better performance with huge files, consider combining it with XMLReader:
<?php
$reader = new XMLReader();
$reader->open('large_file.xml');
while ($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'item') {
$dom = new DOMDocument();
$node = $reader->expand($dom);
// Process the node
}
}
?>