PHPackages                             kiwilan/php-xml-reader - 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. kiwilan/php-xml-reader

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

kiwilan/php-xml-reader
======================

PHP package to read XML with nice API.

1.1.0(1y ago)682.5k↑33.2%1[2 PRs](https://github.com/kiwilan/php-xml-reader/pulls)3MITPHPPHP ^8.0CI passing

Since Jun 19Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/kiwilan/php-xml-reader)[ Packagist](https://packagist.org/packages/kiwilan/php-xml-reader)[ Docs](https://github.com/kiwilan/php-xml-reader)[ GitHub Sponsors](https://github.com/kiwilan)[ RSS](/packages/kiwilan-php-xml-reader/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (16)Used By (3)

PHP XML Reader
==============

[](#php-xml-reader)

[![Banner with cards catalog picture in background and PHP XML Reader title](https://raw.githubusercontent.com/kiwilan/php-xml-reader/main/docs/banner.jpg)](https://raw.githubusercontent.com/kiwilan/php-xml-reader/main/docs/banner.jpg)

[![php](https://camo.githubusercontent.com/24497661b47295454ce326eaecc10393e3a1ef912a1e5b00d70deefe413e1c86/68747470733a2f2f696d672e736869656c64732e696f2f7374617469632f76313f7374796c653d666c6174266c6162656c3d504850266d6573736167653d76382e3026636f6c6f723d373737424234266c6f676f3d706870266c6f676f436f6c6f723d666666666666266c6162656c436f6c6f723d313831383162)](https://www.php.net/)[![version](https://camo.githubusercontent.com/bde3141ab2453dd1810b8ca2bafa8149706eda4b41a79139a900cd534aaac8d4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6b6977696c616e2f7068702d786d6c2d7265616465722e7376673f7374796c653d666c617426636f6c6f72413d31383138314226636f6c6f72423d373737424234)](https://packagist.org/packages/kiwilan/php-xml-reader)[![downloads](https://camo.githubusercontent.com/b35767f72d7a323ac583c6182aa6a83dcba087713768af7c2b6967056558607c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6b6977696c616e2f7068702d786d6c2d7265616465722e7376673f7374796c653d666c617426636f6c6f72413d31383138314226636f6c6f72423d373737424234)](https://packagist.org/packages/kiwilan/php-xml-reader)[![license](https://camo.githubusercontent.com/25e2db896c714e2dc561d13fb36396ad1fa971faae892520a0bdb247a6453f76/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6b6977696c616e2f7068702d786d6c2d7265616465722e7376673f7374796c653d666c617426636f6c6f72413d31383138314226636f6c6f72423d373737424234)](https://github.com/kiwilan/php-xml-reader/blob/main/README.md)[![tests](https://camo.githubusercontent.com/c70fa02f0c4ff02b83ed61085b0917ac16d7885332eb63f1547b20c06fa7e2f6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6b6977696c616e2f7068702d786d6c2d7265616465722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c617426636f6c6f72413d313831383142)](https://packagist.org/packages/kiwilan/php-xml-reader)[![codecov](https://camo.githubusercontent.com/0ecedd6198abb3625e13715f5552f8baa8278bf09c43e14f9e53d1707843878f/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f67682f6b6977696c616e2f7068702d786d6c2d7265616465722f6d61696e3f7374796c653d666c617426636f6c6f72413d31383138314226636f6c6f72423d373737424234)](https://codecov.io/gh/kiwilan/php-xml-reader)

PHP package to read XML with nice API, heavily inspired by [`stackoverflow answer`](https://stackoverflow.com/a/46349713/11008206).

About
-----

[](#about)

PHP have native functions to read XML files with [`SimpleXML`](https://www.php.net/manual/en/book.simplexml.php), but it's not easy to use and not very flexible. This package offer to read XML files as array, with a simple and flexible API.

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

[](#requirements)

- **PHP version** &gt;= *8.0*

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

[](#installation)

You can install the package via composer:

```
composer require kiwilan/php-xml-reader
```

Usage
-----

[](#usage)

You can read XML from path or from content.

- `mapContent`: `boolean` If a key has only `@content` key, return only the value of `@content`. Default: `true`.
- `failOnError`: `boolean` Throw exception if XML is invalid. Default: `true`.

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml', bool $mapContent = true, bool $failOnError = true);
```

### Methods

[](#methods)

MethodDescriptionType`$xml->getRoot()`Value of root element`?string``$xml->getRootNS()`Namespaces of root element`string[]``$xml->getRootAttributes()`Attributes of root element`array``$xml->getRootAttributes('key');`Value of `key` attribute of root element`mixed``$xml->getVersion();`XML version`?string``$xml->getEncoding();`XML encoding`?string``$xml->isValidXml();`Check if XML is valid`bool``$xml->getPath();`Path of XML file`?string``$xml->getFilename();`Filename of XML file`?string``$xml->getConverter();`Converter used to convert XML to array`\Kiwilan\XmlReader\XmlConverter``$xml->save('path/to/file.xml');`Save XML file`bool``$xml->toArray();`Convert XML to array`array``$xml->__toString();`Convert XML to string`string`### Advanced methods

[](#advanced-methods)

MethodDescriptionTypeOptions`$xml->getContents()`XML as multidimensional array`array``$xml->find('key')`Find key will return first value where that contain key`array``strict`, `content`, `attributes``$xml->search('key')`Search will return all values that contain key`mixed``$xml->extract(...keys)`Extract `metadata` key, if not found return `null``mixed``XmlReader::parseContent(key)`Parse content of entry`mixed``XmlReader::parseAttributes(key)`Parse attributes of entry`array` or `null`### Basic usage

[](#basic-usage)

XML as multidimensional array from `root` (safe).

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$contents = $xml->getContents();
```

Basic usage.

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$contents = $xml->getContents();
$title = $contents['metadata']['dc:title'] ?? null;
```

### Find

[](#find)

Find key will return first value where key that contain `title` (safe).

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$title = $xml->find('title', strict: false);
```

Find key will return first value where key is `dc:title` (safe)

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$dcTitle = $xml->find('dc:title');
```

Find key will return first value where key that contain `dc:title` and return `@content` (safe)

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$dcCreator = $xml->find('dc:creator', content: true);
```

Find key will return first value where key contain `dc:creator` and return `@attributes` (safe)

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$dcCreator = $xml->find('dc:creator', attributes: true);
```

### Search

[](#search)

Search will return all values that contain `dc` (safe)

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$dc = $xml->search('dc');
```

### Extract

[](#extract)

Extract `metadata` key, if not found return null (safe)

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$rootKey = $xml->extract('metadata');
```

Extract `metadata` and `dc:title` keys (safe)

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$subSubKey = $xml->extract(['metadata', 'dc:title']);
```

### Get content

[](#get-content)

If you want to extract only `@content` you could use `parseContent()` method, if you want to extract only `@attributes` you could use `parseAttributes()` method.

Extract `dc:title` key and return `@content` (safe)

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$title = $xml->extract(['metadata', 'dc:title']);
$title = XmlReader::parseContent($title);
```

Extract `dc:creator` key and return `@content` (safe)

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$creator = $xml->extract(['metadata', 'dc:creator']);
$creator = XmlReader::parseContent($creator);
```

Extract `dc:creator` key and return `@attributes` (safe)

```
use Kiwilan\XmlReader\XmlReader;

$xml = XmlReader::make('path/to/file.xml');
$creatorAttributes = $xml->extract(['metadata', 'dc:creator']);
$creatorAttributes = XmlReader::parseAttributes($creatorAttributes);
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [`spatie`](https://github.com/spatie) for `spatie/package-skeleton-php`

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

[![](https://user-images.githubusercontent.com/48261459/201463225-0a5a084e-df15-4b11-b1d2-40fafd3555cf.svg)](https://github.com/kiwilan)

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance58

Moderate activity, may be stable

Popularity39

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 77% 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 ~39 days

Recently: every ~116 days

Total

13

Last Release

592d ago

Major Versions

0.2.31 → 1.0.02023-08-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/58eb34eac9af07c3352e83060e472e1c280838ebe0568692561c00a2cfde9e57?d=identicon)[ewilan-riviere](/maintainers/ewilan-riviere)

---

Top Contributors

[![ewilan-riviere](https://avatars.githubusercontent.com/u/48261459?v=4)](https://github.com/ewilan-riviere "ewilan-riviere (67 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (12 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (8 commits)")

---

Tags

phpxmlphpxml

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/kiwilan-php-xml-reader/health.svg)

```
[![Health](https://phpackages.com/badges/kiwilan-php-xml-reader/health.svg)](https://phpackages.com/packages/kiwilan-php-xml-reader)
```

###  Alternatives

[m1/vars

Vars is a simple to use and easily extendable configuration loader with in built loaders for ini, json, PHP, toml, XML and yaml/yml file types. It also comes with in built support for Silex and more frameworks to come soon.

69124.2k1](/packages/m1-vars)[goetas/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

493.3k](/packages/goetas-xsd2php-runtime)[bupy7/xml-constructor

The array-like constructor of XML document structure.

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

PHPackages © 2026

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