PHPackages                             andrewfenn/xmlreader - 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. andrewfenn/xmlreader

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

andrewfenn/xmlreader
====================

andrewfenn/xmlreader is an XML library that makes sabre/xml package easier to use

1.0.0(9y ago)022.9k↑60.7%BSD-3-ClausePHPPHP &gt;=5.5.5

Since Jan 30Pushed 9y ago1 watchersCompare

[ Source](https://github.com/andrewfenn/XMLReaderElement)[ Packagist](https://packagist.org/packages/andrewfenn/xmlreader)[ Docs](https://github.com/andrewfenn/XMLReaderElement)[ RSS](/packages/andrewfenn-xmlreader/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

XMLReaderElement extension for Sabre/XML
========================================

[](#xmlreaderelement-extension-for-sabrexml)

[![Software License](https://camo.githubusercontent.com/b60331a2084501dc07cf6d6964c0da58dd005d89c45cf3b28b4b22b60f5ec00f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4253442d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/0b8f1e7333c3400c7279be007f903cbc32c190dea73bf06348d239058bc74d0e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616e6472657766656e6e2f584d4c526561646572456c656d656e742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/andrewfenn/XMLReaderElement/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/9536d0117d01c510239c687877a27419c81bea3c8cbbb709884e290049e66266/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64642f616e6472657766656e6e2f786d6c7265616465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewfenn/xmlreader)[![Total Downloads](https://camo.githubusercontent.com/67c47650394b3ec459bc2a0b6143ffb1efd786db4af6e11acf6b9490bc54ffc4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f616e6472657766656e6e2f786d6c7265616465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewfenn/xmlreader)[![Total Downloads](https://camo.githubusercontent.com/ce3a0d7f415f2eb8633c658deee316d7a6f7a236b84c9c4ff6e1f5596d41c166/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e6472657766656e6e2f786d6c7265616465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewfenn/xmlreader)

The sabre/xml library is a specialized XML reader and writer for PHP which can be downloaded [here](http://sabre.io/xml/). This project is not associated with the sabre/xml project.

Sabre XML is a great PHP library for XML reading, but it can be difficult to use and make your code unessesarily complicated when you have very simple XML to parse. Therefore I have made this small extension on top of sabre/xml that allows you to access the information more natually making it easier to get what you want done.

Install
-------

[](#install)

`composer require andrewfenn/xmlreader -v ~1.0.0`

Methods
-------

[](#methods)

`XMLReaderElement` provides the following methods...

- **`array find( string )`**

    Searches through all the children or attributes, and returns an array. If you are searching for a tag the array will be a list of XMLReaderElement elements. If you are searching for an attribute your array will be of the value it contains. To search for an attribute prepend an @ to the beginning of your search term.
- **`mixed findFirst( string )`**

    Searches through all the children or attributes and returns the first result found. If you are searching for a tag the result will be an XMLReaderElement. If you are searching for an attribute you will get that attributes value.
- **`array children( void )`**

    Returns an array of this XMLReaderElement's children.
- **`bool hasChildren( void )`**

    If the element has any XMLReaderElement children

Examples
--------

[](#examples)

```
$input =
  string(12) "HotelMessage"
  ["namespace"]=>
  string(0) ""
  ["attributes"]=>
  object(stdClass)#69 (2) {
    ["Version"]=>
    string(3) "1.0"
    ["TimeStamp"]=>
    string(19) "2016-05-26T11:11:50"
  }
  ["children"]=>
  string(19) "Message,Hotel,Hotel"
}
*/
```

### Accessing a single child tag

[](#accessing-a-single-child-tag)

```
echo "Message: ".$data->Message->value."\n";
echo "Message Type: ".$data->Message->attributes->Type."\n";

/* Returns...
Message: Hello
Message Type: Info
*/
```

When accessing a single child tag like this it will return the first element it finds, for multiple children see below.

```
echo "Hotel ID: ".$data->Hotel->attributes->HotelCode."\n";
/* Returns...
Hotel ID: 3
*/
```

### Accessing multiple children tags

[](#accessing-multiple-children-tags)

```
foreach($data->find('Hotel') as $hotel) {
    // Find a specific element's attribute
    echo "Hotel ID: ".$hotel->attributes->HotelCode."\n";
}

foreach($data->children() as $tag) {
    if ($tag->name == 'Hotel') {
        echo "Hotel ID: ".$tag->attributes->HotelCode."\n";
    }
}

/* Returns...
Hotel ID: 3
Hotel ID: 7
Hotel ID: 3
Hotel ID: 7
*/
```

### Finding an array of tags or attributes

[](#finding-an-array-of-tags-or-attributes)

The `find()` function will always return an array. Your search term must match exactly to that of the Tag or Attribute.

```
// You can search for any tag attribute by prepending an @ infront of the attribute's name like so...
foreach($data->find('@HotelCode') as $hotel_code) {
    echo "Hotel Code: ".$hotel_code."\n";
}

// You can search for any tag like so...
foreach($data->find('LengthsOfStay') as $los) {
    echo "Length of Stay: ".$los->findFirst('@Time')." Days\n";
}

/* Returns...
Hotel Code: 3
Hotel Code: 4
Length of Stay: 3 Days
Length of Stay: 7 Days
*/
```

### Picking up the first element or attribute

[](#picking-up-the-first-element-or-attribute)

```
echo $data->findFirst('@TimeStamp')."\n";
echo $data->findFirst('@HotelCode')."\n";
echo $data->findFirst('Hotel')->findFirst('@RatePlanCode')."\n";

/* Returns...
2016-05-26T11:11:50
3
888
*/
```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

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

3396d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6a30960104ca833b308b6678e8b8f5579ec5a1706f0097c4820136d538d494fd?d=identicon)[andrewfenn](/maintainers/andrewfenn)

---

Tags

xmldomXMLReaderXMLWriter

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/andrewfenn-xmlreader/health.svg)

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

###  Alternatives

[sabre/xml

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

52832.2M131](/packages/sabre-xml)[masterminds/html5

An HTML5 parser and serializer.

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

A fluent api for the php dom extension.

337306.9k17](/packages/fluentdom-fluentdom)[veewee/xml

XML without worries

1835.9M29](/packages/veewee-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)[rct567/dom-query

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

134261.0k4](/packages/rct567-dom-query)

PHPackages © 2026

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