decodelabs / exemplar
Powerful XML tools
Installs: 1 367
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- decodelabs/coercion: ^0.2
- decodelabs/collections: ^0.9
- decodelabs/elementary: ^0.4
- decodelabs/exceptional: ^0.4
- decodelabs/glitch-support: ^0.4
Requires (Dev)
- decodelabs/atlas: ^0.12
- decodelabs/phpstan-decodelabs: ^0.6
Suggests
- decodelabs/atlas: Save generated XML to file
README
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.
Installation
composer require decodelabs/exemplar
Usage
Reading & manipulating
Access and manipulate XML files with a consolidated interface wrapping the DOM functionality available in 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 for the full interface.
Writing
Programatically generate XML output with a full-featured wrapper around PHP's XML Writer:
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 version="1.0" encoding="UTF-8"?> <ns:section ns:attr1="value" class="test"> <title id="main">This is a title</title> <body><![CDATA[This is an element with content wrapped in CDATA tags.]]></body> <![CDATA[This is plain CDATA]]></ns:section>
See Writer.php for the full interface.
Licensing
Exemplar is licensed under the MIT License. See LICENSE for the full license text.