8fold / php-xml-builder
A library for building XML document and element strings.
Requires
- php: ^8.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^1.2
- phpstan/extension-installer: *
- phpstan/phpstan: ^2.0.0
- phpstan/phpstan-strict-rules: ^2.1
- phpunit/phpunit: ^9.6 || ^10.0 || ^11.0 || ^12.0 || ^13.0
- spaze/phpstan-disallowed-calls: ^4.15
- squizlabs/php_codesniffer: ^4.0
- symfony/process: ^5.4 || ^6.0 || ^7.4 || ^8.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
XML Builder is designed to build a string, not a document object model (DOM) or abstract syntax tree (AST).
Warning: Users of this library are responsible for sanitizing content.
For DOM or AST, there are other libraries and native implementations (ex. PHP:DOM and SimpleXML).
You might use XML Builder to generate a string you feed into either PHP:DOM or Simple XML. Or, use the string as the body of an HTTP response.
Use the Element class to create individual nodes within the document.
Use the Document class to generate the doctype declaration and root level element.
Installation
composer require 8fold/php-xml-builder
Usage
Element
The fundamental building block for all output.
use Eightfold\XmlBuilder\Element; // non-empty element, using `create()` Element::create('xml_tag', 'Hello, World!')->props('id intro');
Output:
<xml_tag id="intro">Hello, World!</p>
Non-empty element using magic method:
Element::xml_tag('Hello, World!')->props('id intro');
Output:
<xml_tag id="intro">Hello, World!</p>
Empty element using magic method:
Element::xml_tag()->omitEndTag()->props('src /path/to/image.png');
Output:
<xml_tag src="/path/to/image.php" />
EmptyElement
The EmptyElement class was introduced in the 4.0 release and is designed to mirror the input shorthand of HTML those elements compared to using the Element class to do the same.
use Eightfold\XmlBuilder\Element; use Eightfold\XmlBuilder\EmptyElement; Element::xml_tag()->omitEndTag()->props('src /path/to/image.png');
Output:
<xml_tag src="/path/to/image.php" />
Using EmptyElement:
EmptyElement::xml_tag('src /path/to/image.png'); // output same as previous
Document
The Document class gives you to generate a complete XML document entirely in PHP.
use Eightfold\XmlBuilder\Document; use Eightfold\XmlBuilder\Element; use Eightfold\XmlBuilder\EmptyElement; use Eightfold\XmlBuilder\Cdata; echo Document::root( Element::child( EmptyElement::grandchild('name Xavier'), Cdata::create('Hello, my name is Xavier!') ) );
Output
<!-- compressed --> <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <root><child><grandchild name="Xavier"/><![CDATA[Hello, my name is Xavier!]]></child></root> <!-- human-readable --> <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <root> <child> <grandchild name="Xavier"/> <![CDATA[Hello, my name is Xavier!]]> </child> </root>
Further examples are in the tests folder with an example extension.
Cdata
The Cdata class allows you to insert literal text within an XML document.
See Document.
Comment
There’s even a Comment component for adding inline XML comments.
use Eightfold\XmlBuilder\Document; use Eightfold\XmlBuilder\Element; use Eightfold\XmlBuilder\EmptyElement; use Eightfold\XmlBuilder\Cdata; use Eightfold\XmlBuilder\Comment; echo Document::root( Element::child( EmptyElement::grandchild('name Xavier'), Comment::create('This is a comment') ) );
<!-- compressed --> <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <root><child><grandchild name="Xavier"/><!-- This is a comment --></child></root> <!-- human-readable --> <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <root> <child> <grandchild name="Xavier"/> <!-- This is a comment --> </child> </root>
Extending
Each XML Builder component accepts string literals and classes that implement the PHP-native Stringable interface. Further, XML Builder provides the PropertyInterface, which extends the Stringable interface. Therefore, XML Builder allows developers to create custom components and APIs without extending XML Builder’s base components.
Compatibility
| XML Builder version | PHP version |
|---|---|
| 4+ | 8.0+ |
| 3+ | 8.4+ |
| 1+ | 8.0+ |
| 0+ | 7.4+ |
Note: v3 to v4 opens support to backward compatibility on PHP versions; mainly we determined that the PHP 8.4+ capabilities were unnecessary — at least for now.
Details
See 8fold HTML Builder for PHP HTML Builder’s sibling.
The origins of this library was an experiment where PHP was viewed as a "pure" programming language, not a template engine that grew into being a language.
The primary pain point was to maintain the feel of writing human-readable XML (tabs and spaces) while reducing the risk of human error; specifically, mismatched beginning and end tags.
The secondary pain point was that PHP:DOM and SimpleXML felt cumbersome when generating XML and HTML documents.