PHPackages                             saloonphp/xml-wrangler - 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. saloonphp/xml-wrangler

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

saloonphp/xml-wrangler
======================

Easily Read &amp; Write XML in PHP

v1.5.0(1mo ago)4222.3M—0%16[4 issues](https://github.com/saloonphp/xml-wrangler/issues)[2 PRs](https://github.com/saloonphp/xml-wrangler/pulls)16MITPHPPHP ^8.2CI passing

Since Oct 28Pushed 1mo ago6 watchersCompare

[ Source](https://github.com/saloonphp/xml-wrangler)[ Packagist](https://packagist.org/packages/saloonphp/xml-wrangler)[ Docs](https://github.com/saloonphp/xml-wrangler)[ GitHub Sponsors](https://github.com/sammyjo20)[ RSS](/packages/saloonphp-xml-wrangler/feed)WikiDiscussions v1 Synced 1mo ago

READMEChangelog (10)Dependencies (20)Versions (24)Used By (16)

🌵 XML Wrangler - Easily Read &amp; Write XML in PHP
---------------------------------------------------

[](#-xml-wrangler---easily-read--write-xml-in-php)

XML Wrangler is a simplistic PHP library designed to make reading and writing XML easy. XML Wrangler has been built with developer experience in mind - you can read any type of XML file, even with complex namespaces and even large XML files. It will also throw exceptions if the XML is invalid!

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

[](#installation)

XML Wrangler is installed via Composer.

```
composer require saloonphp/xml-wrangler

```

> Requires PHP 8.1+

Reading XML
-----------

[](#reading-xml)

Reading XML can be done by passing the XML string or file into the XML reader and using one of the many methods to search and find a specific element or value. You can also convert every element into an easily traversable array if you prefer. If you need to access attributes on an element you can use the `Element` DTO which is a simple class to access the content and attributes. XML Wrangler provides methods to iterate through multiple elements while only keeping one element in memory at a time.

```

    Belgian Waffles
    $5.95
    Two of our famous Belgian Waffles with plenty of real maple syrup
    650

    Strawberry Belgian Waffles
    $7.95
    Light Belgian waffles covered with strawberries and whipped cream
    900

    Berry-Berry Belgian Waffles
    $8.95
    Light Belgian waffles covered with an assortment of fresh berries and whipped cream
    900

```

```
 **Warning**Due to limitations of the underlying PHP XMLReader class, the `fromStream`, `fromPsrResponse` and `fromSaloon` methods will create a temporary file on your machine/server to read from which will be automatically removed when the reader is destructed. You will need to ensure that you have enough storage on your machine to use this method.

#### Converting Everything Into An Array

[](#converting-everything-into-an-array)

You can use the `elements` and `values` methods to convert the whole XML document into an array. If you would like an array of values, use the `values` method - but if you need to access attributes on the elements, the `elements` method will return an array of `Element` DTOs.

```
$reader = XmlReader::fromString(...);

$elements = $reader->elements(); // Array of `Element::class` DTOs

$values = $reader->values(); // Array of values.
```

> **Note**If you are reading a large XML file, you should use the `element` or `value` methods instead. These methods can iterate through large XML files without running out of memory.

#### Reading Specific Values

[](#reading-specific-values)

You can use the `value` method to get a specific element's value. You can use dot-notation to search for child elements. You can also use whole numbers to find specific positions of multiple elements. This method searches through the whole XML body in a memory-efficient way.

This method will return a `LazyQuery` class which has different methods on to retrieve the data.

```
$reader = XmlReader::fromString('

        Sammyjo20

            Luke Combs - When It Rains It Pours
            Sam Ryder - SPACE MAN
            London Symfony Orchestra - Starfield Suite

');

$reader->value('person.name')->sole() // 'Sammyjo20'

$reader->value('song')->get(); // ['Luke Combs - When It Rains It Pours', 'Sam Ryder - SPACE MAN', ...]

$reader->value('song.2')->sole(); // 'London Symfony Orchestra - Starfield Suite'
```

#### Reading Specific Elements

[](#reading-specific-elements)

You can use the `element` method to search for a specific element. This method will provide an `Element` class which contains the value and attributes. You can use dot-notation to search for child elements. You can also use whole numbers to find specific positions of multiple elements. This method searches through the whole XML body in a memory efficient way.

This method will return a `LazyQuery` class which has different methods to retrieve the data.

```
$reader = XmlReader::fromString('

        Sammyjo20

            Luke Combs - When It Rains It Pours
            Sam Ryder - SPACE MAN
            London Symfony Orchestra - Starfield Suite

');

$reader->element('name')->sole(); // Element('Sammyjo20')

$reader->element('song')->get(); // [Element('Luke Combs - When It Rains It Pours'), Element('Sam Ryder - SPACE MAN'), ...]

$reader->element('song.2')->sole(); // Element('London Symfony Orchestra - Starfield Suite')
```

#### Removing Namespaces From XML

[](#removing-namespaces-from-xml)

Sometimes it's easier to traverse an XML document when you don't have to worry about namespaces and prefixes to elements. If you would like to remove them you can use the `removeNamespaces()` method on the reader.

```
$reader = XmlReader::fromString(...);

$reader->removeNamespaces();
```

#### Lazily Iterating

[](#lazily-iterating)

When searching a large file, you can use the `lazy` or `collectLazy` methods which will return a generator of results only keeping one item in memory at a time.

```
$names = $reader->element('name')->lazy();

foreach ($names as $name) {
    //
}
```

#### Using Laravel Collections

[](#using-laravel-collections)

If you are using Laravel, you can use the `collect` and `collectLazy` methods which will convert the elements into a Laravel Collection/Lazy Collection. If you are not using Laravel, you can install the `illuminate/collections` package via Composer to add this functionality.

```
$names = $reader->value('name')->collect();

$names = $reader->value('name')->collectLazy();
```

#### Searching by specific attributes

[](#searching-by-specific-attributes)

Sometimes you might want to search for a specific element or value where the element contains a specific attribute. You can do this by providing a second argument to the `value` or `element` method. This will search the last element for the attributes and will return if they match.

```
$reader = XmlReader::fromString('

        Sammyjo20

            Luke Combs - When It Rains It Pours
            Sam Ryder - SPACE MAN
            London Symfony Orchestra - Starfield Suite

');

$reader->element('song', ['recent' => 'true'])->sole(); // Element('London Symfony Orchestra - Starfield Suite')

$reader->value('song', ['recent' => 'true'])->sole(); // 'London Symfony Orchestra - Starfield Suite'
```

### Reading with XPath

[](#reading-with-xpath)

XPath is a fantastic way to search through XML. With one string, you can search for a specific element, with specific attributes or indexes. If you are interested in learning XPath, you can [click here for a useful cheatsheet](https://devhints.io/xpath).

#### Reading Specific Values via XPath

[](#reading-specific-values-via-xpath)

You can use the `xpathValue` method to find a specific element's value with an XPath query. This method will return a `Query` class which has different methods to retrieve the data.

```
`.

#### Adding custom "Processing Instructions" to the XML

[](#adding-custom-processing-instructions-to-the-xml)

You can add a custom "Processing Instruction" to the XML by using the `addProcessingInstruction` method.

```
use Saloon\XmlWrangler\XmlWriter;

$writer = new XmlWriter();
$writer->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="base.xsl"');

$xml = $writer->write('root', ['name' => 'Sam']);
```

This will result in the following XML

```

  Sam

```

#### Minification

[](#minification)

By default the XML written is not minified. You can provide the third argument to the `write` method to minify the XML.

```
use Saloon\XmlWrangler\XmlWriter;

$xml = XmlWriter::make()->write('root', [...], minified: true);
```

Credits
-------

[](#credits)

XML Wrangler is a simple wrapper around two really powerful libraries which do a lot of the legwork. Both of libraries are fantastic and deserve a star!

- [veewee/xml](https://github.com/veewee/xml) - Used for reading and decoding XML, but has a powerful writing engine of its own.
- [spatie/array-to-xml](https://github.com/spatie/array-to-xml) - A brilliant library to convert an array into XML

### Other Credits

[](#other-credits)

- [Sam Carré](https://github.com/sammyjo20)
- [Toon Verwerft](https://github.com/veewee)
- [Spatie](https://github.com/spatie)

###  Health Score

65

—

FairBetter than 99% of packages

Maintenance89

Actively maintained with recent releases

Popularity61

Solid adoption and visibility

Community31

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 90.7% 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 ~44 days

Recently: every ~150 days

Total

21

Last Release

54d ago

Major Versions

v0.2.3 → v1.0.02023-11-12

PHP version history (2 changes)v0.0.1PHP ^8.1

v1.4.1PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/de41c8bfde9d0edb25da9fdc159283fe9442e3dc9b651ee9aa7ded8384e5629d?d=identicon)[SamCarre](/maintainers/SamCarre)

---

Top Contributors

[![Sammyjo20](https://avatars.githubusercontent.com/u/29132017?v=4)](https://github.com/Sammyjo20 "Sammyjo20 (136 commits)")[![bmckay959](https://avatars.githubusercontent.com/u/7681836?v=4)](https://github.com/bmckay959 "bmckay959 (4 commits)")[![jbraband](https://avatars.githubusercontent.com/u/1907283?v=4)](https://github.com/jbraband "jbraband (3 commits)")[![ash-jc-allen](https://avatars.githubusercontent.com/u/39652331?v=4)](https://github.com/ash-jc-allen "ash-jc-allen (3 commits)")[![SanderMuller](https://avatars.githubusercontent.com/u/9074391?v=4)](https://github.com/SanderMuller "SanderMuller (2 commits)")[![fredbradley](https://avatars.githubusercontent.com/u/1639226?v=4)](https://github.com/fredbradley "fredbradley (1 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (1 commits)")

---

Tags

phpxmlxml-parserxml-writer

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/saloonphp-xml-wrangler/health.svg)

```
[![Health](https://phpackages.com/badges/saloonphp-xml-wrangler/health.svg)](https://phpackages.com/packages/saloonphp-xml-wrangler)
```

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[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)[mck89/peast

Peast is PHP library that generates AST for JavaScript code

18834.7M29](/packages/mck89-peast)

PHPackages © 2026

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