https://github.com/danmichaelo/quitesimplexmlelement
Small wrapper around PHP's SimpleXMLElement
https://github.com/danmichaelo/quitesimplexmlelement
Last synced: 8 months ago
JSON representation
Small wrapper around PHP's SimpleXMLElement
- Host: GitHub
- URL: https://github.com/danmichaelo/quitesimplexmlelement
- Owner: danmichaelo
- License: mit
- Created: 2013-07-04T11:31:57.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2018-07-29T15:27:07.000Z (almost 8 years ago)
- Last Synced: 2025-05-06T07:03:59.673Z (about 1 year ago)
- Language: PHP
- Homepage:
- Size: 68.4 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# QuiteSimpleXMLElement
[](https://travis-ci.org/danmichaelo/quitesimplexmlelement)
[](https://coveralls.io/r/danmichaelo/quitesimplexmlelement?branch=master)
[](https://scrutinizer-ci.com/g/danmichaelo/quitesimplexmlelement/?branch=master)
[](https://insight.sensiolabs.com/projects/2654313d-e4ea-48dd-90c0-c13863d10c3a)
[](https://packagist.org/packages/danmichaelo/quitesimplexmlelement)
[](https://packagist.org/packages/danmichaelo/quitesimplexmlelement)
The `QuiteSimpleXMLElement` class is a small wrapper built around the `SimpleXMLElement` class that adds some convenience methods and makes it easier to work with namespaces. The main reason for developing the class was to let objects returned by the `xpath()` method inherit namespaces from the original object. The package was formerly known as `CustomXMLElement`.
QuiteSimpleXMLElement supports PHP 5.6 and 7.x. If you need PHP 5.3 support, use the `0.4.*` version range as PHP 5.3 support was removed in version 0.5.
The library is actively maintained and pull requests are welcome.
## Why this library was developed
Taking an example document,
```php
$xml = '
1.9
';
```
Using SimpleXMLElement I found myself having to register namespaces over and over again:
```php
$root = new SimpleXMLElement($xml);
$root->registerXPathNamespace('d', 'http://purl.org/dc/elements/1.1/');
$a = $root->xpath('d:a');
$a[0]->registerXPathNamespace('d', 'http://purl.org/dc/elements/1.1/');
$b = $a[0]->xpath('d:b');
```
When using QuiteSimpleXMLElement, it should only be necessary to register the namespaces once and for all.
```php
$node = new QuiteSimpleXMLElement($xml);
$node->registerXPathNamespace('d', 'http://purl.org/dc/elements/1.1/');
$a = $node->xpath('d:a');
$b = $a->xpath('d:b');
```
The namespaces can also be defined using the alternative constructor `QuiteSimpleXMLElement::make`:
```php
$node = QuiteSimpleXMLElement::make($xml, ['d' => 'http://purl.org/dc/elements/1.1/']);
$a = $node->xpath('d:a');
$b = $a->xpath('d:b');
```
A note on the design: I would have preferred to extend the original SimpleXMLElement class, but the constructor is static, which is why I wrote a wrapper instead.
## Helper methods
The library defines some new methods to support less typing and cleaner code.
### attr($name)
Returns the value of an attribute as a string. Namespace prefixes are supported.
```php
echo $node->attr('id');
```
### text($xpath)
Returns the text content of the node
```php
echo $node->text('d:a/d:b');
```
### first($xpath)
Returns the first node that matches the given path, or null if none.
```php
$node = $node->first('d:a/d:b');
```
#### all($xpath)
Returns all nodes that matches the given path, or an empty array if none.
```php
$node = $node->all('d:a/d:b');
```
### has($xpath)
Returns true if the node exists, false if not
```php
if ($node->has('d:a/d:b') {
…
}
```
### setValue($value)
Sets the value of a node
```php
$node->setValue('Hello world');
```
### replace($newNode)
Replaces the current node with a new one. Example:
```php
$book = new QuiteSimpleXMLElement('
Chapter one
Chapter two
');
$introduction = new QuiteSimpleXMLElement('
Introduction
');
$firstChapter = $book->first('chapter');
$firstChapter->replace($introduction);
```
gives
```xml
Introduction
Chapter two
```
Works with namespaces as well, but any namespaces used in the replacement node
must be specified in that document as well. See `QuiteSimpleXMLElementTest.php`
for an example.