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

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

vierbergenlars/xml
==================

0.3.2(11y ago)0661MITPHP

Since Dec 29Pushed 11y ago1 watchersCompare

[ Source](https://github.com/vierbergenlars/xml)[ Packagist](https://packagist.org/packages/vierbergenlars/xml)[ RSS](/packages/vierbergenlars-xml/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (6)Used By (1)

XML
===

[](#xml)

[![Build Status](https://camo.githubusercontent.com/98f30c6df2372334d4f9fe3fb45dda9aa88bff36cf7bc427578784e28bb61035/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f7669657262657267656e6c6172732f786d6c2e706e67)](http://travis-ci.org/vierbergenlars/xml)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/2d65ab2f37770826a03cfdd0436a0079e1afae1804aec76ced5602f5d4e465eb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7669657262657267656e6c6172732f786d6c2f6261646765732f7175616c6974792d73636f72652e706e673f733d36656339323733636565663031313930616661306538653232383465393034666235316461386164)](https://scrutinizer-ci.com/g/vierbergenlars/xml/)[![Latest Stable Version](https://camo.githubusercontent.com/ba70eb62e2db7dc1151c6b167525e788a77743d443c331cc215ca068061f2f6b/68747470733a2f2f706f7365722e707567782e6f72672f7669657262657267656e6c6172732f786d6c2f762f737461626c652e706e67)](https://packagist.org/packages/vierbergenlars/xml)

A sane wrapper around [SimpleXML](http://be2.php.net/manual/en/book.simplexml.php)

- [Installation](#installation)
- [Usage](#usage)
- [Reference](#reference)
    - [`XmlElementInterface`](#xmlelementinterface)
    - [`XmlCollectionInterface`](#xmlcollectioninterface)
    - [`XmlAttributesInterface`](#xmlattributesinterface)
- [License](#license)

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

[](#installation)

`composer require vierbergenlars/xml:@stable`

> **Protip:** you should browse the [`vierbergenlars/xml`](https://packagist.org/packages/vierbergenlars/xml)page to choose a stable version to use, avoid the `@stable` meta constraint.

Usage
-----

[](#usage)

Only the `XmlElement` class should be instanciated in your code.

```
use vierbergenlars\Xml\XmlElement;

$simpleXml = new \SimpleXMLElement($xml);
$xmlElement = new XmlElement($simpleXml);
```

The examples are based around following xml structure:

```

    Screenshot from 2013-10-21 19:31:42.png]]>
    Algebra]]>
    Exercise]]>

    Screen Shot 2013-11-17 at 13.34.30.xcf]]>
    Analyse]]>
    Slides]]>

```

> This example is a copy of [example.php](https://github.com/vierbergenlars/xml/blob/master/example.php), which is available in the repository for experimentation.

```
$totalPages = ceil($xmlElement->attr('total') / $xmlElement->attr('items_per_page'));
echo $xmlElement->attr('page') . '/' . $totalPages . "\n";
```

`XmlElement::attr($name)` gets the value of the XML attribute `$name` on the current element.

```
$files = $xmlElement->children();
/* @var $files XmlCollectionInterface */
```

`XmlElement::children()` gets the immediate children of the current element. The result implements `XmlCollectionInterface`. It is an `Iterator`, implements `ArrayAccess` and `Countable`.

For easier chaining in PHP 5.3, a `$XmlCollectionInterface->get($idx)` function is provided, which has the same effect as `$XmlCollectionInterface[$idx]`.

A `->find($attrs = array())` function is provided that returns a new `XmlCollectionInterface`, which only contains elements with the attributes passed to the function.

```
foreach($files as $file) {
    /* @var $file XmlElementInterface */
    echo 'File ' . $file->attr('id') . ":\n";
    echo '  Filename: ' . $file->child('filename')->text() . "\n";
```

The first element is selected, and the text between its tags is returned (with `->text()`).`

```
    echo "  Links:\n";
    foreach($file->children('link') as $link) {
```

Here, the children of `$file` are filtered to contain only elements with tagname ``.

```
        /* @var $link XmlElementInterface */
        echo '    ' . $link->attr('href');
        foreach($link->attributes() as $attr => $value) {
```

All attributes from the `` element are selected. `$link->attributes()` returns an implementation of `XmlAttributesInterface`. The object acts as a read-only `array` with attribute names as keys and attribute values as value. Additionally, it has a `->get($idx)` function for easier chaining.

```
            if($attr != 'href')
                echo ' (' . $attr . '=' . $value . ')';
        }
        echo "\n";
    }

    echo '  Weblink: ' . $file->children('link')->find(array('rel'  => 'self', 'type' => 'www'))->get(0)->attr('href') . "\n";
```

This is an example of using `XmlCollectionInterface::find()` to only select the one `` element with the desired attributes. An equivalent using `XmlElementInterface::child()` is commented-out below.

```
//    echo '  Weblink: ' . $file->child('link', array('rel'  => 'self', 'type' => 'www'))->attr('href') . "\n";
}

echo 'Total: ' . $files->count() . "\n";
```

Reference
---------

[](#reference)

### `XmlElementInterface`

[](#xmlelementinterface)

ReturnFunction signatureDocumentation`string``getName()`Get the element tag name`string``text()`Get the text contained in the element`XmlElementInterface``setText(string $text)`Set the text contained in the element to `$text`. Returns itself.`string``attr(string $name)`Get the value of the attribute `$name` on the element (equivalent to `->attributes()->get($name)`)`XmlAttributesInterface``attributes()`Gets all attributes on the element`XmlElementInterface``child(string $name = null, array $filter = array(), int $n = 0)`Gets the `$n`th child with the specified name (equivalent to `->children($name)->find($filter)->pos($n)`)`XmlElementInterface``addChild(string $name)`Adds a child with tagname `$name` to the current element. The returned element can be modified.`XmlElementInterface``addChild(string $name, string $text)`Adds a child with tagname `$name` and text content `$text` (`->addChild($name)->setText($text)`)`XmlElementInterface``addChild(string $name, XmlElementInterface $element)`Adds a child with tagname `$name` and sets its content and attributes to the same as `$element``XmlElementInterface``addChild(XmlElementInterface $element)`Adds a child to the current element, with tagname, content and attributes the same as `$element``XmlCollectionInterface``children(string $name = null)`Get all children of the element (or only those with the specified name if `$name !== null`)`XmlCollectionInterface``find(array $attributes = array())`Get the children of the element whose attributes match those in `$attributes`.### `XmlCollectionInterface`

[](#xmlcollectioninterface)

Implements interfaces `Iterator`, `ArrayAccess`, `Countable`

ReturnFunction signatureDocumentation`XmlElementInterface``get(int $pos)`Get the element at the given position`XmlElementInterface``add($name, $value = null)`Calls `XmlElementInterface::addChild()` on the parent element`XmlCollectionInterface``find(array $attributes = array())`Filters members of the collection on their attributes`int``count()``Countable::count()` Count the elements in the collection`boolean``offsetExists(int $offset)``ArrayAccess::offsetExists` Whether a offset exists (see `get()`)`XmlElementInterface``offsetGet(int $offset)``ArrayAccess::offsetGet()` Offset to retrieve-`offsetSet()`Throws `LogicException`. The collection cannot be modified in this way-`offsetUnset(int $offset)``ArrayAcess:offsetUnset()` Removes element at `$offset` from collection`XmlElementInterface``current()``Iterator::current()` Return the current element`int``key()``Iterator::key()` Return key of the current element`void``next()``Iterator::next()` Move forward to next element`void``rewind()``Iterator::rewind()` Rewind the iterator to the first element`boolean``valid()``Iterator::valid()` Checks if current position is valid1. This object implements `ArrayAccess`:

- `$xmlCollectionInterface[$offset]` calls `$xmlCollectionInterface->offsetGet($offset)`
- `isset($xmlCollectionInterface[$offset])` calls `$xmlCollectionInterface->offsetExists($offset)`
- `$xmlCollectionInterface[$offset] = $mixed` calls `$xmlCollectionInterface->offsetSet($offset, $mixed)` and throws an exception.
- `unset($xmlCollectionInterface[$offset])` calls `$xmlCollectionInterface->offsetUnset($offset)`

2. This object implements `Countable`:

- `count($xmlCollectionInterface)` calls `$xmlCollectionInterface->count()`

3. This object implements `Iterator`

- `foreach($xmlCollectionInterface as $key => $value){}` calls `$xmlCollectionInterface->rewind()` once, `$xmlCollectionInterface->valid()` before each iteration, and if it returns true sets `$key = $xmlCollectionInterface->key(); $value = $xmlCollectionInterface->current()`. Finally calls `$xmlCollectionInterface->next()`.

### `XmlAttributesInterface`

[](#xmlattributesinterface)

Implements interfaces `Iterator`, `ArrayAccess`, `Countable`

ReturnFunction signatureDocumentation`string` or `null``get(string $name)`Get the value of the attribute `$name``XmlAttributesInterface``set(string $name, strinv $value)`Sets attribute `$name` to value `$value`. Returns itself`int``count()``Countable::count()` Count the elements in the collection`boolean``offsetExists(string $attr)``ArrayAccess::offsetExists()` Whether an attribute exists`string` or `null``offsetGet(string $attr)``ArrayAccess::offsetGet()` Attribute to retrieve (see `get()`)-`offsetSet(string $attr, string $val )``ArrayAccess:offsetSet()` Sets attribute to a value (see `set()`)-`offsetUnset(string $attr)``ArrayAccess::offsetUnset()` Removes attribute from element`string``current()``Iterator::current()` Return the current element`string``key()``Iterator::key()` Return key of the current element`void``next()``Iterator::next()` Move forward to next element`void``rewind()``Iterator::rewind()` Rewind the iterator to the first element`boolean``valid()``Iterator::valid()` Checks if current position is valid1. This object implements `ArrayAccess`:

    - `$xmlAttributesInterface[$offset]` calls `$xmlAttributesInterface->offsetGet($offset)`
    - `isset($xmlAttributesInterface[$offset])` calls `$xmlAttributesInterface->offsetExists($offset)`
    - `$xmlAttributesInterface[$offset] = $mixed` calls `$xmlAttributesInterface->offsetSet($offset, $mixed)`
    - `unset($xmlAttributesInterface[$offset])` calls `$xmlAttributesInterface->offsetUnset($offset)`
2. This object implements `Countable`:

    - `count($xmlAttributesInterface)` calls `$xmlAttributesInterface->count()`
3. This object implements `Iterator`

    - `foreach($xmlAttributesInterface as $key => $value){}` calls `$xmlAttributesInterface->rewind()` once, `$xmlAttributesInterface->valid()` before each iteration, and if it returns true sets `$key = $xmlAttributesInterface->key(); $value = $xmlAttributesInterface->current()`. Finally calls `$xmlAttributesInterface->next()`.

License
-------

[](#license)

[MIT](https://github.com/vierbergenlars/xml/blob/master/LICENSE.md)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

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

Total

5

Last Release

4349d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/18169985c64a307450da907595cc928bec99e316c0b8da5114b2708691ad8e6d?d=identicon)[vierbergenlars](/maintainers/vierbergenlars)

---

Top Contributors

[![vierbergenlars](https://avatars.githubusercontent.com/u/1194648?v=4)](https://github.com/vierbergenlars "vierbergenlars (17 commits)")

### Embed Badge

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

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

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M284](/packages/opis-closure)[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)[michelf/php-markdown

PHP Markdown

3.5k52.4M345](/packages/michelf-php-markdown)[jms/metadata

Class/method/property metadata management in PHP

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

PHPackages © 2026

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