PHPackages                             borislavsabev/simple-xml-loader - 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. borislavsabev/simple-xml-loader

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

borislavsabev/simple-xml-loader
===============================

A minimalistic SimpleXml loader. Wraps the simplexml\_load\_XXXX() methods and provides error handling.

v1.0.0(8y ago)51.8k↓50%2GPL-3.0PHP

Since Jul 3Pushed 8y ago2 watchersCompare

[ Source](https://github.com/BorislavSabev/SimpleXmlLoader)[ Packagist](https://packagist.org/packages/borislavsabev/simple-xml-loader)[ Docs](https://github.com/BorislavSabev/SimpleXmlLoader)[ RSS](/packages/borislavsabev-simple-xml-loader/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

SimpleXMLLoader
===============

[](#simplexmlloader)

A minimalistic SimpleXml loader. Wraps the simplexml\_load\_XXXX() methods and provides libxml error handling.

Main goals:
-----------

[](#main-goals)

- provide a transparent OOP interface to SimpleXML's `simplexml_load_file()` and `simplexml_load_string()` functions
- handle SimpleXML's internal errors
- throw catchable Exceptions (XmlLoaderException)

This wrapper will:

- try to load your XML resource into a \\SimpleXMLElement object and return it
- handle any LibXML errors that occur during loading

This wrapper will not:

- do anything else

Installation
============

[](#installation)

This package is available on [Packagist](https://packagist.org/packages/borislavsabev/simple-xml-loader) and installable via Composer:

`composer require borislavsabev/simple-xml-loader`

Basic usage
===========

[](#basic-usage)

As this is a wrapper the wrapped library's functionality is not covered in this documentation. Please refer to:

- [The SimpleXml manual](http://php.net/manual/en/book.simplexml.php)
- [The SimpleXml functions reference](http://php.net/manual/en/ref.simplexml.php)

There are two main functions in the SimpleXMlLoader:

```
public function loadFile($filename, $xmlClass = "SimpleXMLElement", $options = 0, $xmlNamespace = "", $isPrefix = false)

public function loadString($data, $xmlClass = "SimpleXMLElement", $options = 0, $xmlNamespace = "", $isPrefix = false)

```

The loader throws XmlLoaderException on any error that occurs. The exception thrown contains the message and code of the last \\LibXmlError. The loader calls are meant to be wrapped in try/catch blocks and read any errors yourself:

```
   use BorislavSabev\SimpleXmlLoader\XmlLoader;
   use BorislavSabev\SimpleXmlLoader\Exception\XmlLoaderException;

   $xmlLoader = new XmlLoader();
   try {
       /** @var \SimpleXMLElement $simpleXmlElement */
       $simpleXmlElement = $xmlLoader->loadFile($filename);

       //Now do something with the loaded \SimpleXMLElement...
   } catch (XmlLoaderException $e) {
       // The last error is contained in $e:
       $e->getMessage();
       $e->getCode();
       //All errors accessible through the payload:
       /** @var array $xmlErrors */
       $xmlErrors = $xmlLoader->getXmlPayload()
                              ->getXmlErrors();
   }

```

The XmlLoader instance is meant to be reused thus:

- Each call to `XmlLoader->loadFile()` or `XmlLoader->loadString()` will clear any LibXml errors stored
- Each call to `XmlLoader->loadFile()` or `XmlLoader->loadString()` will replace it's internal XmlLoaderPayload object

Loop usage
----------

[](#loop-usage)

Each consecutive call to a loader method resets the state of LibXML and the payload you must extract all data between calls.

```
   use BorislavSabev\SimpleXmlLoader\XmlLoader;
   use BorislavSabev\SimpleXmlLoader\Exception\XmlLoaderException;

   $xmlLoader = new XmlLoader();

   foreach ($aBunchOfXmlStrings as $xmlString) {
       try {
            /** @var \SimpleXMLElement $simpleXmlElement */
            $simpleXmlElement = $xmlLoader->loadString($xmlString);
       } catch (XmlLoaderException $e) {
           /** @var array $xmlErrors */
           $xmlErrors = $xmlLoader->getXmlPayload()
                                  ->getXmlErrors();
       }

       //Any data within LibXML or our XmlLoaderPayload will be lost after this iteration of the loop
   }

```

Reusing XmlLoaderPayload
------------------------

[](#reusing-xmlloaderpayload)

You can also reuse the XmlLoaderPayload in your application if you which. Say for example you use this wrapper to parse different XML file but then want to pass the result to different Services in your code that will handle any business logic. You could just get the XmlLoaderPayload and pass it along to a specific service (or whatever really):

```
   use BorislavSabev\SimpleXmlLoader\XmlLoader;
   use BorislavSabev\SimpleXmlLoader\Exception\XmlLoaderException;

   $xmlLoader = new XmlLoader();
   try {
       $xmlLoader->loadFile($filename);

       //Generic example:
       $serviceBroker->pass(
           MyCoolService::class,
           $xmlLoader->getXmlPayload()
       );
   } catch (XmlLoaderException $e) {
       /** @var array $xmlErrors */
       $xmlErrors = $xmlLoader->getXmlPayload()
                              ->getXmlErrors();
   }

```

**NOTE:** Generally you should have you own payload objects to pass data around in your Domain. The idea of XmlLoaderPayload is to be internal for XmlLoader thus it cannot contain any logic outside of that task.

Behaviour overwritten
=====================

[](#behaviour-overwritten)

There is only one behaviour change that this library introduces:
*"If you pass an empty string to `simplexml_load_string()` it will return false and throw no error"*.
The SimpleXmlLoader wrapper throws an `XmlLoaderException` in this case.

The XmlLoaderException and LibXML's libXMLError
===============================================

[](#the-xmlloaderexception-and-libxmls-libxmlerror)

XmlLoaderException's codes are specific to this wrapper. LibXML's libXMLError objects are just returned in an array and as received from SimpleXML/LibXML.

Personal Opinion on the SimpleXML PHP Extension
===============================================

[](#personal-opinion-on-the-simplexml-php-extension)

Generally SimpleXML is not a solid PHP extension and, in my mind, it should be used rarely when you need to do something simple fast. Any serious work should be done via DomDocument.
The previous two sentences are the author's personal opinion which as with any opinion should be taken with a grain of salt.

Versioning
----------

[](#versioning)

This library follows [Semver](http://semver.org). According to Semver, you will be able to upgrade to any minor or patch version of this library without any breaking changes to the public API. Semver also requires that we clearly define the public API for this library.
All methods, with `public` visibility, are part of the public API. All other methods are not part of the public API. Where possible, we'll try to keep `protected` methods backwards-compatible in minor/patch versions, but if you're overriding methods then please test your work before upgrading.

Contributing
============

[](#contributing)

Please do! PR's are very welcome. The author is far from thinking that this wrapper library is perfect. Any PR must first abide by the \[Main goals\](#Main goals) set out by this library.

Please note that this project adheres to the [Contributor Covenant v1.4](http://contributor-covenant.org/). By participating in this project you agree to abide by its terms.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

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 ~19 days

Total

2

Last Release

3221d ago

Major Versions

v0.8 → v1.0.02017-07-22

### Community

Maintainers

![](https://www.gravatar.com/avatar/7aca1429033f54a4fb64668cabe7e9ed94f2ea5cb5f3b18613202e0813de575c?d=identicon)[BorislavSabev](/maintainers/BorislavSabev)

---

Top Contributors

[![BorislavSabev](https://avatars.githubusercontent.com/u/1708306?v=4)](https://github.com/BorislavSabev "BorislavSabev (10 commits)")

---

Tags

xmlSimpleXMLXmlLoaderSimpleXmlLoader

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/borislavsabev-simple-xml-loader/health.svg)

```
[![Health](https://phpackages.com/badges/borislavsabev-simple-xml-loader/health.svg)](https://phpackages.com/packages/borislavsabev-simple-xml-loader)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[jms/serializer

Library for (de-)serializing data of any complexity; supports XML, and JSON.

2.3k135.8M851](/packages/jms-serializer)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[sabre/xml

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

52832.2M131](/packages/sabre-xml)[dkrnl/simplexmlreader

Wrapper XMLReader class, for simple SAX-reading(and simple XPath-queries) of huge(testing over 1G file) xml.

112951.5k](/packages/dkrnl-simplexmlreader)

PHPackages © 2026

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