lightools / xml
Simple and safe parsing of XML and HTML sources.
Requires
- php: >=8.4
- ext-dom: *
- ext-libxml: *
Requires (Dev)
- editorconfig-checker/editorconfig-checker: ^10.7.0
- phpstan/phpstan: ^2.1.54
- phpstan/phpstan-strict-rules: ^2.0.11
- phpunit/phpunit: ^13.3
- slevomat/coding-standard: ^8.28.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-08-26 14:59:57 UTC
README
This library provides simple interface for loading XML or HTML strings to Dom\XMLDocument or Dom\HTMLDocument objects (the DOM API introduced in PHP 8.4).
It prevents some known vulnerabilities and allows you to handle LibXML errors simply by catching XmlException as you can see below.
Installation
$ composer require lightools/xml
Simple usage
The loadXml method returns Dom\XMLDocument and the loadHtml method returns Dom\HTMLDocument (parsed by the spec-compliant HTML5 parser).
The HTML5 parser recovers from malformed markup, so loadHtml throws only for empty input.
If you prefer working with SimpleXmlElement, you can use simplexml_import_dom function.
$xml = '<?xml version="1.0"?><root>text</root>'; $html = '<!doctype html><title>Foo</title>'; $loader = new Lightools\Xml\XmlLoader(); try { $xmlDocument = $loader->loadXml($xml); $htmlDocument = $loader->loadHtml($html); } catch (Lightools\Xml\XmlException $e) { // process exception }
How to run checks
$ composer check
Versions
- v1.x is for PHP 5.4 and higher
- v2.x is for PHP 7.1 and higher
- v3.x is for PHP 8.0 and higher
- v4.x is for PHP 8.4 and higher (returns
Dom\XMLDocument/Dom\HTMLDocumentinstead ofDOMDocument)