PHPackages                             josemmo/uxml - 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. josemmo/uxml

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

josemmo/uxml
============

Uncomplicated XML manipulation library with a clean and concise syntax

v0.2.0(5mo ago)7392.2k↑11%24MITPHPPHP &gt;=7.1CI passing

Since Oct 3Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/josemmo/uxml)[ Packagist](https://packagist.org/packages/josemmo/uxml)[ Docs](https://github.com/josemmo/uxml)[ RSS](/packages/josemmo-uxml/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)Dependencies (5)Versions (9)Used By (4)

Uncomplicated XML
=================

[](#uncomplicated-xml)

[![Build Status](https://github.com/josemmo/uxml/workflows/CI/badge.svg)](https://github.com/josemmo/uxml/actions)[![Latest Version](https://camo.githubusercontent.com/afc83d7f68cb4ca3370163d4635188329f985898dc18a3009d665d696431c89b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6f73656d6d6f2f75786d6c)](https://packagist.org/packages/josemmo/uxml)[![Minimum PHP Version](https://camo.githubusercontent.com/0556094a0d3a986ed0decef64567bdd7828892be42bf54cae9216e125226f18c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6a6f73656d6d6f2f75786d6c)](#installation)[![License](https://camo.githubusercontent.com/f92f6d801a4df12b6035fc376b322fa51ae4e71c51ddb44021bbc5b5922d78db/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a6f73656d6d6f2f75786d6c)](LICENSE)

UXML is an *extremely* simple PHP library for manipulating XML documents with ease while keeping overhead to a bare minimum.

It consist of just a single class which uses the PHP built-in `DOMElement` and `DOMDocument` classes under the hood.

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

[](#installation)

### Using Composer

[](#using-composer)

```
composer require josemmo/uxml

```

### Without Composer

[](#without-composer)

Download source files from the GitHub repository:

```
git clone https://github.com/josemmo/uxml.git

```

Use the UXML class in your app:

```
use UXML\UXML;
require_once __DIR__ . "/uxml/src/UXML.php";
```

FAQ
---

[](#faq)

**Why use this instead of [sabre/xml](https://github.com/sabre-io/xml) or [FluidXML](https://github.com/servo-php/fluidxml)?**
Both those options are great and if they fit your project you should definetely use them! However, in my case I needed something more lightweight to put on top of [LibXML's DOM](https://www.php.net/manual/en/book.dom.php) to provide an alternative syntax.

**Is UXML compatible with `DOMElement`?**
Yes, indeed! You can get the original `DOMElement` instance from an `UXML` object and vice versa.

**I want UXML to do "X". Can you implement it?**
My main goal with this project is not to implement all possible behaviors in the XML specification, you can use the DOM or SimpleXML libraries for that.

Usage
-----

[](#usage)

### Create a new document

[](#create-a-new-document)

UXML does not distinguish between XML documents (`DOMDocument`) and elements (`DOMElement`). Instead, you can create a new document like so:

```
$xml = \UXML\UXML::newInstance('RootTagName');
```

You can also wrap an already existing `DOMElement`:

```
$domElement = new DOMElement('TagName');
$xml = \UXML\UXML::fromElement($domElement);
```

### Load an XML document from source

[](#load-an-xml-document-from-source)

By loading an XML string, UXML will return the root element of the document tree:

```
$source = add('Child');
echo $child; //
echo $xml;   //
```

You can also define a value:

```
$child = $xml->add('Child', 'Hello World!');
echo $child; // Hello World!
```

And even attributes or namespaces:

```
$feed = \UXML\UXML::newInstance('feed', null, [
    'xmlns' => 'http://www.w3.org/2005/Atom'
]);
echo $feed; //

$link = $feed->add('link', 'Wow!', [
    'href' => 'https://www.example.com'
]);
echo $link; // Wow!
```

### Element chaining

[](#element-chaining)

Because with every element insertion a reference to the new element is returned, you can chain multiple of these calls to create a tree:

```
$xml = \UXML\UXML::newInstance('people');
$xml->add('person')->add('name', 'Jane Doe');
echo $xml; //
           //
           //         Jane Doe
           //
           //
```

### Export XML source

[](#export-xml-source)

Besides casting `UXML` objects to a `string`, there is a method for exporting the XML source of an element and its children:

```
$xml->asXML();
```

By default, exported strings include an XML declaration (except when casting `UXML` instances to a `string`).

### Find XML elements

[](#find-xml-elements)

UXML allows you to use XPath 1.0 queries to get a particular element from a document:

```
$xml = \UXML\UXML::newInstance('person');
$xml->add('name', 'Jane');
$xml->add('surname', 'Doe');
$xml->add('color', 'green', ['hex' => '#0f0']);

echo $xml->get('*[@hex]'); // green
var_dump($xml->get('birthday')); // NULL
```

Or even multiple elements:

```
$xml = \UXML\UXML::fromString('123');
foreach ($xml->getAll('b') as $elem) {
    echo "Element says: " . $elem->asText() . "\n";
}
```

Note all XPath queries are **relative to current element**:

```
$source = get('director')->get('year'); // 1970
echo $xml->get('year'); // 2010
echo $xml->get('director')->get('//year'); // 2010
```

### Remove XML elements

[](#remove-xml-elements)

Elements can be removed from the XML tree by calling the `remove()` method on them. After an element is removed, it becomes unusable:

```
$source = remove();
echo $xml; // Alpha
```

### Namespaces

[](#namespaces)

Namespaces are assigned in the same way as other attributes:

```
$xml->add('TagName', null, [
    'xmlns' => 'https://example.com',
    'xmlns:abc' => 'urn:abc',
    'attribute' => 'value'
])->add('abc:Child', 'Name');
echo $xml; //
           //     Name
           //
```

However, when querying elements, the prefix defined in the document may not be the one you are expecting:

```
$xml = \UXML\UXML::fromString('');
echo $xml->get('ns:b'); //
echo $xml->get('abc:b'); // Is NULL as the prefix does not exist
```

To fix this, you can make use of [clark notation](https://sabre.io/xml/clark-notation/) inside the XPath query:

```
echo $xml->get('{urn:abc}b'); //
```

### Advanced XML manipulation

[](#advanced-xml-manipulation)

For any other document manipulation outside the scope of this library, you can always interact with the `DOMElement` instance:

```
$xml = \UXML\UXML::newInstance('Test');
$xml->element(); // Returns a [DOMElement] object
```

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance71

Regular maintenance activity

Popularity43

Moderate usage in the ecosystem

Community14

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

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

Recently: every ~396 days

Total

8

Last Release

162d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/35634d9b598826205bb05ac611f5e0b322a79a1fdec13168ca0019d584e87450?d=identicon)[josemmo](/maintainers/josemmo)

---

Top Contributors

[![josemmo](https://avatars.githubusercontent.com/u/4470267?v=4)](https://github.com/josemmo "josemmo (53 commits)")

---

Tags

phpxmlxml-manipulationxml-parserxml

###  Code Quality

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/josemmo-uxml/health.svg)

```
[![Health](https://phpackages.com/badges/josemmo-uxml/health.svg)](https://phpackages.com/packages/josemmo-uxml)
```

###  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)[vipnytt/sitemapparser

XML Sitemap parser class compliant with the Sitemaps.org protocol.

772.2M10](/packages/vipnytt-sitemapparser)

PHPackages © 2026

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