PHPackages                             ottosmops/xmltoolkit - 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. ottosmops/xmltoolkit

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

ottosmops/xmltoolkit
====================

Add and remove XML tags and attributes.

v1.0.0(1y ago)042MITPHPPHP ^8.3CI passing

Since Mar 19Pushed 9mo ago1 watchersCompare

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

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

[![Build Status](https://github.com/ottosmops/xmltoolkit/actions/workflows/ci.yaml/badge.svg)](https://github.com/ottosmops/xmltoolkit/actions)[![License](https://camo.githubusercontent.com/a679a450b94b85986c73d76e38eea98080a4f6a89a81a3d4cb5aedbc045aa4bb/68747470733a2f2f706f7365722e707567782e6f72672f6f74746f736d6f70732f786d6c746f6f6c6b69742f6c6963656e7365)](https://packagist.org/packages/ottosmops/xmltoolkit)[![Total Downloads](https://camo.githubusercontent.com/cf4456b42cd3d98f157a7d357f6d5bc691b5456c6ce48c78c0cf156f09ca02ac/687474703a2f2f706f7365722e707567782e6f72672f6f74746f736d6f70732f786d6c746f6f6c6b69742f646f776e6c6f616473)](https://packagist.org/packages/ottosmops/xmltoolkit)

Xmltoolkit is a PHP class that provides various utilities for manipulating XML documents. It allows loading XML from files or strings, performing XPath queries (with namespace support), and modifying XML elements and attributes.

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

[](#installation)

Use Composer to install the package:

```
composer require ottosmops/xmltoolkit
```

Usage
-----

[](#usage)

$elements = $xml-&gt;findElementsByAttributeRegex('type', '/^sp.*/'); $xml-&gt;replaceAttributeValueRegex('type', '/^sp.*/', 'normal'); $elements = $xml-&gt;findElementsByTextRegex('/old.*/'); $xml-&gt;replaceElementTextRegex('/old.*/', 'newtext');

```
require 'vendor/autoload.php';
use Ottosmops\Xmltoolkit;

$xml = new Xmltoolkit();
$xml->loadFromFile('example.xml');
$xml->renameTagByXPath('//a', 'ref');
$xml->saveToFile('example.xml', true); // pretty print

// Find elements by attribute value
$elements = $xml->findElementsByAttributeValue('type', 'special');

// Replace attribute value
$xml->replaceAttributeValue('type', 'special', 'normal');

// Find elements by text content
$elements = $xml->findElementsByTextContent('oldtext');

// Replace text content
$xml->replaceElementTextContent('oldtext', 'newtext');

// Save as pretty-printed string
$prettyXml = $xml->saveToString(true);
```

Namespace Support
-----------------

[](#namespace-support)

You can register XML namespaces for XPath queries:

```
$xml = new Ottosmops\Xmltoolkit();
$xml->registerNamespaces(['ns' => 'http://example.com/ns']);
$xml->loadFromFile('example.xml');
$nodes = $xml->queryXPath('//ns:tag');
```

Methods
-------

[](#methods)

...existing code...

Load an XML file into the DOM object and ensure it is UTF-8 encoded.

```
public function loadFromFile(string $filePath): bool
```

Load an XML string into the DOM object and ensure it is UTF-8 encoded.

```
public function loadFromString(string $xmlString): bool
```

Load an XML/HTML Fragment

```
public function loadFromFragment(string $xmlFragment): bool
```

Save the XML document to a file and ensure it is UTF-8 encoded.

```
public function saveToFile(string $filePath, bool $prettyPrint = false): bool
```

Returns the XML as a string and ensures it is UTF-8 encoded.

```
public function saveToString(bool $prettyPrint = false): string
```

Find elements by attribute value.

```
public function findElementsByAttributeValue(string $attributeName, string $attributeValue): array
```

Replace attribute value for all elements with a given attribute value.

```
public function replaceAttributeValue(string $attributeName, string $oldValue, string $newValue): void
```

Find elements by text content.

```
public function findElementsByTextContent(string $textContent): array
```

Replace text content for all elements with a given text content.

```
public function replaceElementTextContent(string $oldText, string $newText): void
```

Rename a tag found by an XPath expression.

```
public function renameTagByXPath(string $xpathExpression, string $newTagName):
void
```

Rename an attribute found by an XPath expression.

```
public function renameAttributeByXPath(string $xpathExpression, string $oldAttributeName, string $newAttributeName): void
```

Add a new attribute to a tag found by an XPath expression.

```
public function addAttributeByXPath(string $xpathExpression, string $attributeName, string $attributeValue): void
```

Remove an attribute from a tag found by an XPath expression.

```
public function removeAttributeByXPath(string $xpathExpression, string $attributeName): void
```

Does an arbitrary XPath query and returns the found nodes as an array.

```
public function queryXPath(string $xpathExpression): array
```

Inserts an HTML string after the nodes found by an XPath expression.

```
public function appendHtmlToXPath(string $xpathExpression, string $htmlString): void
```

Removes an element found by an XPath expression.

```
    public function removeElementByXPath(string $xpathExpression): void
```

Wrap an element found by an XPath expression with a new element.

```
public function wrapElementByXPath(string $xpathExpression, string $wrapperTagName): void
```

Remove an element but keep its content (unwrap the element) based on an XPath expression.

```
public function unwrapElementByXPath(string $xpathExpression): void
```

Tests
-----

[](#tests)

Regex Features
--------------

[](#regex-features)

### Methods

[](#methods-1)

Find elements by attribute value using regex:

```
public function findElementsByAttributeRegex(string $attributeName, string $pattern): array
```

Replace attribute value using regex:

```
public function replaceAttributeValueRegex(string $attributeName, string $pattern, string $replacement): void
```

Find elements by text content using regex:

```
public function findElementsByTextRegex(string $pattern): array
```

Replace text content using regex:

```
public function replaceElementTextRegex(string $pattern, string $replacement): void
```

### Usage

[](#usage-1)

```
$elements = $xml->findElementsByAttributeRegex('type', '/^sp.*/');
$xml->replaceAttributeValueRegex('type', '/^sp.*/', 'normal');
$elements = $xml->findElementsByTextRegex('/old.*/');
$xml->replaceElementTextRegex('/old.*/', 'newtext');
```

### Tests

[](#tests-1)

Run the tests with PHPUnit:

```
vendor/bin/phpunit
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance51

Moderate activity, may be stable

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

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

Unknown

Total

1

Last Release

425d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/76f31e7e6772db47a91388ed82840fae1fa57185bb82a64924bb3839697222c2?d=identicon)[ottosmops](/maintainers/ottosmops)

---

Top Contributors

[![ottosmops](https://avatars.githubusercontent.com/u/4144389?v=4)](https://github.com/ottosmops "ottosmops (7 commits)")

---

Tags

phpxmlxmltoolkit

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ottosmops-xmltoolkit/health.svg)

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

###  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)
