theseer/fdomdocument
Archived PHP library extending DOMDocument to throw exceptions instead of warnings/notices, plus handy shortcuts like XPath query helpers and appendElement variants. Drop-in replacement for DOM with libxml support; works with PHP 5.3.3–8.1 (v1.6.7).
composer require theseer/fdomdocument:^1.6
DOMDocument with fDOMDocument:
use TheSeer\fDOM\fDOMDocument;
$dom = new fDOMDocument();
$dom->loadXML('<root><child/></root>');
try {
$dom->loadXML('<invalid>xml</invalid>');
} catch (fDOMException $e) {
// Log or handle error (no PHP warnings)
Log::error($e->getMessage());
}
$dom = new fDOMDocument();
try {
$dom->loadXML($xmlString);
$nodes = $dom->query('//book'); // XPath query
foreach ($nodes as $node) {
echo $node->nodeValue;
}
} catch (fDOMException $e) {
// Structured error handling
abort(500, 'XML parsing failed: ' . $e->getMessage());
}
Usage Samples).DOMDocument but add shortcuts like queryOne(), select() (CSS), and appendElement().XmlParserService) or Blade components for dynamic XML/HTML generation.try-catch blocks to avoid PHP warnings.try {
$dom->loadHTML($html);
$title = $dom->queryOne('//title')->nodeValue;
} catch (fDOMException $e) {
$title = 'Default Title';
}
select() for jQuery-like syntax (faster than XPath for simple queries).$links = $dom->select('a[href^="https://"]');
foreach ($links as $link) {
$link->setAttribute('target', '_blank');
}
appendElement() shortcuts to reduce boilerplate.$root = $dom->appendElement('catalog');
$root->appendElement('product', null, null, 'name="Laptop"');
fDOMXPath.$xpath = $dom->createXPath();
$query = $xpath->prepare('//item[@id=:id]');
$query->bindValue('id', 123);
$item = $query->queryOne();
__toString() for quick output.$dom = new fDOMDocument();
$dom->appendElement('response')->appendTextNode('Success');
return response($dom->saveHTML(), 200);
class XmlParserService {
public function parse(string $xml): array {
$dom = new fDOMDocument();
try {
$dom->loadXML($xml);
return $this->extractData($dom);
} catch (fDOMException $e) {
Log::error($e->getMessage());
return [];
}
}
}
Blade::directive('xml', function ($expression) {
$dom = new fDOMDocument();
eval("\$dom->$expression");
return "<?php echo \$dom->saveXML(); ?>";
});
Usage:
@xml('appendElement("user")->appendElement("name", null, null, "John")')
FormRequest:
public function rules() {
return [
'xml_data' => ['required', function ($attribute, $value, $fail) {
$dom = new fDOMDocument();
try {
$dom->loadXML($value);
} catch (fDOMException $e) {
$fail('Invalid XML: ' . $e->getMessage());
}
}],
];
}
$response = new fDOMDocument();
$response->appendElement('data')->appendTextNode($data);
return response($response->saveXML(), 200, ['Content-Type' => 'application/xml']);
fDOMException::setFullMessage(false) to reduce exception payload size:
fDOMException::setFullMessage(false); // Disable full error details
select() uses a subset of CSS selectors (not full jQuery syntax).Symfony/CSSSelector.null where int is expected).1.6.7 (includes fixes for PHP 8.1).fDOMDocument extends DOMDocument, but some IDEs may not recognize the extended methods.@method PHPDoc annotations or configure your IDE to recognize the package’s classes.fDOMDocument may break XPath queries (fixed in 1.3.2+).>=1.3.2.&) may still trigger warnings despite exceptions.libxml_use_internal_errors(true) before loading:
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
fDOMException::setFullMessage(true); // For detailed error debugging
try {
$dom->loadXML($xml);
} catch (fDOMException $e) {
Log::debug('DOM Error', ['message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
}
if (!filter_var($xml, FILTER_VALIDATE_BOOLEAN, FILTER_FLAG_NO_ENCODED_SPACES)) {
throw new \InvalidArgumentException('Invalid XML/HTML');
}
$xpath = $dom->createXPath(); // Reuse for multiple queries
$nodes = $xpath->query('//item');
// Bad: Query twice
$dom->query('//title');
$dom->query('//title');
// Good: Cache results
$titles = $dom->query('//title');
queryOne() for Single Nodes$title = $dom->queryOne('//title'); // Faster than query() + first()
fDOMException::setExceptionHandler(function (fDOMException $e) {
report($e); // Laravel's error reporting
abort(500, 'DOM Error: ' . $e->getMessage());
});
How can I help you explore Laravel packages today?