https://github.com/decodelabs/exemplar
Powerful XML tools for PHP
https://github.com/decodelabs/exemplar
php xml
Last synced: about 1 year ago
JSON representation
Powerful XML tools for PHP
- Host: GitHub
- URL: https://github.com/decodelabs/exemplar
- Owner: decodelabs
- License: mit
- Created: 2021-03-31T13:01:32.000Z (about 5 years ago)
- Default Branch: develop
- Last Pushed: 2025-04-14T08:47:08.000Z (about 1 year ago)
- Last Synced: 2025-04-15T14:07:09.967Z (about 1 year ago)
- Topics: php, xml
- Language: PHP
- Homepage:
- Size: 123 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Exemplar
[](https://packagist.org/packages/decodelabs/exemplar)
[](https://packagist.org/packages/decodelabs/exemplar)
[](https://packagist.org/packages/decodelabs/exemplar)
[](https://github.com/decodelabs/exemplar/actions/workflows/integrate.yml)
[](https://github.com/phpstan/phpstan)
[](https://packagist.org/packages/decodelabs/exemplar)
### Powerful XML tools for PHP.
Exemplar provides a set of exhaustive and intuitive interfaces for reading, writing and manipulating XML documents and fragments.
---
## Installation
```bash
composer require decodelabs/exemplar
```
## Usage
### Reading & manipulating
Access and manipulate XML files with a consolidated interface wrapping the DOM functionality available in PHP:
```php
use DecodeLabs\Exemplar\Element as XmlElement;
$element = XmlElement::fromFile('/path/to/my/file.xml');
if($element->hasAttribute('old')) {
$element->removeAttribute('old');
}
$element->setAttribute('new', 'value');
foreach($element->scanChildrenOfType('section') as $sectTag) {
$inner = $sectTag->getFirstChildOfType('title');
$sectTag->removeChild($inner);
// Flatten to plain text
echo $sectTag->getComposedTextContent();
}
file_put_contents('newfile.xml', (string)$element);
```
See [Element.php](./src/Element.php) for the full interface.
### Writing
Programatically generate XML output with a full-featured wrapper around PHP's XML Writer:
```php
use DecodeLabs\Exemplar\Writer as XmlWriter;
$writer = new XmlWriter();
$writer->writeHeader();
$writer->{'ns:section[ns:attr1=value].test'}(function ($writer) {
$writer->{'title#main'}('This is a title');
$writer->{'@body'}('This is an element with content wrapped in CDATA tags.');
$writer->writeCData('This is plain CDATA');
});
echo $writer;
```
This creates:
```xml
This is a title
```
See [Writer.php](./src/Writer.php) for the full interface.
## Licensing
Exemplar is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.