Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/decodelabs/exemplar
Powerful XML tools for PHP
https://github.com/decodelabs/exemplar
php xml
Last synced: 17 days 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 (almost 4 years ago)
- Default Branch: develop
- Last Pushed: 2024-09-04T21:05:38.000Z (5 months ago)
- Last Synced: 2024-12-18T11:45:40.738Z (about 1 month ago)
- Topics: php, xml
- Language: PHP
- Homepage:
- Size: 83 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Exemplar
[![PHP from Packagist](https://img.shields.io/packagist/php-v/decodelabs/exemplar?style=flat)](https://packagist.org/packages/decodelabs/exemplar)
[![Latest Version](https://img.shields.io/packagist/v/decodelabs/exemplar.svg?style=flat)](https://packagist.org/packages/decodelabs/exemplar)
[![Total Downloads](https://img.shields.io/packagist/dt/decodelabs/exemplar.svg?style=flat)](https://packagist.org/packages/decodelabs/exemplar)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/decodelabs/exemplar/integrate.yml?branch=develop)](https://github.com/decodelabs/exemplar/actions/workflows/integrate.yml)
[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-44CC11.svg?longCache=true&style=flat)](https://github.com/phpstan/phpstan)
[![License](https://img.shields.io/packagist/l/decodelabs/exemplar?style=flat)](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.
_Get news and updates on the [DecodeLabs blog](https://blog.decodelabs.com)._
---
## 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.