PHPackages                             matecat/xml-dom-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. matecat/xml-dom-parser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

matecat/xml-dom-parser
======================

Matecat XML DomDocument Loader component

v2.0.0(3mo ago)079.4k↓40.8%2MITPHPPHP &gt;=8.3

Since Apr 23Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/matecat/xml-dom-parser)[ Packagist](https://packagist.org/packages/matecat/xml-dom-parser)[ Docs](https://github.com/matecat/xml-dom-parser)[ RSS](/packages/matecat-xml-dom-parser/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (2)Dependencies (2)Versions (10)Used By (2)

[![Build Status](https://camo.githubusercontent.com/4b19d08748ab74001dc7bb9ba2774c7f25eb09d490ba28ee065697e687f93668/68747470733a2f2f6170702e7472617669732d63692e636f6d2f6d6174656361742f786d6c2d646f6d2d7061727365722e7376673f746f6b656e3d7142617a786b4877503138683345576e486a6a46266272616e63683d6d6173746572)](https://app.travis-ci.com/matecat/xml-dom-parser)![license](https://camo.githubusercontent.com/e84945d33152c410580a8aceed92614069b1372164152d3c48dfaddecdf3e2fb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6d6174656361742f786d6c2d646f6d2d7061727365722e737667)![Packagist](https://camo.githubusercontent.com/ad6c5b18e428683ee52e613c78ade651fe79a3dd7f8c460dcd310ae97199250d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6174656361742f786d6c2d646f6d2d7061727365722e737667)[![Quality Gate Status](https://camo.githubusercontent.com/320734b26b243c2a9e9fbbe985587cba804cad8eba478e697f5cff2de7ffebf5/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d6d6174656361745f786d6c2d646f6d2d706172736572266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/summary/new_code?id=matecat_xml-dom-parser)[![Coverage](https://camo.githubusercontent.com/6070deab6e1ea9bf1121c57fea684d0b75da43187413006c0cc9e224531894bc/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d6d6174656361745f786d6c2d646f6d2d706172736572266d65747269633d636f766572616765)](https://sonarcloud.io/summary/new_code?id=matecat_xml-dom-parser)[![Reliability Rating](https://camo.githubusercontent.com/a504ba1c38e0c0b7aeb9e7c908cd9a778a60258a349d4398a9231c49ba2e94df/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d6d6174656361745f786d6c2d646f6d2d706172736572266d65747269633d72656c696162696c6974795f726174696e67)](https://sonarcloud.io/summary/new_code?id=matecat_xml-dom-parser)[![Maintainability Rating](https://camo.githubusercontent.com/edf85d9ec1d72a52e421f46b46cc78f547f6868cd36e69af1b97e8621dafab5e/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d6d6174656361745f786d6c2d646f6d2d706172736572266d65747269633d7371616c655f726174696e67)](https://sonarcloud.io/summary/new_code?id=matecat_xml-dom-parser)

Matecat XML DOM Parser
======================

[](#matecat-xml-dom-parser)

A lightweight PHP library for parsing XML and HTML documents into traversable object structures. Built on top of PHP's DOMDocument with enhanced error handling and validation support.

Requirements
------------

[](#requirements)

- PHP 8.3+
- ext-dom
- ext-xml
- ext-libxml

Installation
------------

[](#installation)

```
composer require matecat/xml-dom-parser
```

Usage
-----

[](#usage)

### Parsing XML

[](#parsing-xml)

```
use Matecat\XmlParser\XmlParser;

$xml = '

    Tove
    Jani
    Hello!
';

$parsed = XmlParser::parse($xml);

// Access elements
echo $parsed[0]->tagName;              // "to"
echo $parsed[0]->inner_html[0]->text;  // "Tove"
```

### Parsing XML Fragments

[](#parsing-xml-fragments)

```
use Matecat\XmlParser\XmlParser;

$fragment = 'ContentMore content';

$parsed = XmlParser::parse($fragment, isXmlFragment: true);

echo $parsed[0]->attributes['id'];     // "1"
echo $parsed[1]->inner_html[0]->text;  // "More content"
```

### Parsing HTML

[](#parsing-html)

```
use Matecat\XmlParser\HtmlParser;

$html = 'TestContent';

$parsed = HtmlParser::parse($html);

echo $parsed[0]->inner_html[0]->tagName;  // "head"
echo $parsed[0]->inner_html[1]->tagName;  // "body"
```

### Using XmlDomLoader Directly

[](#using-xmldomloader-directly)

For more control over XML loading and validation:

```
use Matecat\XmlParser\XmlDomLoader;
use Matecat\XmlParser\Config;

// Basic loading
$dom = XmlDomLoader::load($xmlContent);

// With configuration
$config = new Config(
    setRootElement: 'root',        // Wrap content in a root element
    allowDocumentType: false,      // Reject DOCTYPE declarations
    xmlOptions: LIBXML_NONET,      // libxml options
    schemaOrCallable: '/path/to/schema.xsd'  // XSD validation
);

$dom = XmlDomLoader::load($xmlContent, $config);
```

### Schema Validation

[](#schema-validation)

Validate XML against an XSD schema:

```
use Matecat\XmlParser\XmlDomLoader;
use Matecat\XmlParser\Config;

$config = new Config(schemaOrCallable: '/path/to/schema.xsd');
$dom = XmlDomLoader::load($xml, $config);
```

Or use a custom validation callable:

```
use Matecat\XmlParser\Config;
use Matecat\XmlParser\XmlDomLoader;
use DOMDocument;

$validator = function (DOMDocument $dom, bool $internalErrors): bool {
    // Custom validation logic
    return $dom->getElementsByTagName('required-element')->length > 0;
};

$config = new Config(schemaOrCallable: $validator);
$dom = XmlDomLoader::load($xml, $config);
```

Parsed Element Structure
------------------------

[](#parsed-element-structure)

Each parsed element is an object with the following properties:

PropertyTypeDescription`node``string`The raw XML/HTML of the element`tagName``string`The element's tag name`attributes``array`Key-value pairs of attributes`text``string|null`Text content (for text nodes)`self_closed``bool|null`Whether the element is self-closing`has_children``bool|null`Whether the element has child nodes`inner_html``ArrayObject`Child elementsException Handling
------------------

[](#exception-handling)

The library throws specific exceptions for different error conditions:

- `XmlParsingException` - XML syntax errors or validation failures
- `InvalidXmlException` - XML is well-formed but invalid against schema
- `DomDependecyMissingException` - Required PHP extensions are missing

```
use Matecat\XmlParser\XmlParser;
use Matecat\XmlParser\Exception\XmlParsingException;

try {
    $parsed = XmlParser::parse('');
} catch (XmlParsingException $e) {
    echo "Parse error: " . $e->getMessage();
}
```

License
-------

[](#license)

This project is licensed under the MIT License.

###  Health Score

50

—

FairBetter than 95% of packages

Maintenance78

Regular maintenance activity

Popularity31

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~94 days

Recently: every ~162 days

Total

8

Last Release

113d ago

Major Versions

v1.1.3 → v2.0.02026-02-16

PHP version history (2 changes)v1.0.0PHP &gt;=5.6

v2.0.0PHP &gt;=8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/54114553?v=4)[MateCat-bot](/maintainers/MateCat-bot)[@MateCat-bot](https://github.com/MateCat-bot)

---

Top Contributors

[![Ostico](https://avatars.githubusercontent.com/u/8008416?v=4)](https://github.com/Ostico "Ostico (18 commits)")

---

Tags

phpxmldom

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/matecat-xml-dom-parser/health.svg)

```
[![Health](https://phpackages.com/badges/matecat-xml-dom-parser/health.svg)](https://phpackages.com/packages/matecat-xml-dom-parser)
```

###  Alternatives

[sabre/xml

sabre/xml is an XML library that you may not hate.

52933.7M139](/packages/sabre-xml)[simplehtmldom/simplehtmldom

A fast, simple and reliable HTML document parser for PHP.

1931.3M15](/packages/simplehtmldom-simplehtmldom)[goetas/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

493.3k](/packages/goetas-xsd2php-runtime)[hexydec/htmldoc

A token based HTML document parser and minifier. Minify HTML documents including inline CSS, Javascript, and SVG's on the fly. Extract document text, attributes, and fragments. Full test suite.

2610.8k4](/packages/hexydec-htmldoc)[bupy7/xml-constructor

The array-like constructor of XML document structure.

1338.9k](/packages/bupy7-xml-constructor)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
