8fold / php-xml-builder
A library for building XML document and element strings.
Installs: 4 816
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: ^8.0
Requires (Dev)
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
README
XML Builder is designed to build a string
, not a document object model (DOM)
or abstract syntax tree (AST).
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
Warning: Users of this library are responsible for sanitizing content.
use Eightfold\XMLBuilder\Document; use Eightfold\XMLBuilder\Element; use Eightfold\XMLBuilder\Cdata; echo Document::create('root', Element::create('child', Element::create('grandchild')->omitEndTag()->props('name Xavier'), Cdata::create('Hello, my name is Xavier!') ) );
Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <root><child><grandchild name="Xavier"/><![CDATA[Hello, my name is Xavier!]]></child></root>
Output (formatted):
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <root> <child> <grandchild name="Xavier"/> <![CDATA[Hello, my name is Xavier!]]> </child> </root>
Alternatively, there is a shorthand variation.
The shorthand method uses the __callStatic
PHP magic method.
use Eightfold\XMLBuilder\Document; use Eightfold\XMLBuilder\Element; use Eightfold\XMLBuilder\Cdata; echo Document::root( Element::child( Element::grandchild()->omitEndTag()->props('name Xavier'), Cdata::create('Hello, my name is Xavier!') ) ); // output: Same as previous example.
Comments are available:
use Eightfold\XMLBuilder\Document; use Eightfold\XMLBuilder\Element; use Eightfold\XMLBuilder\Cdata; echo Document::create( 'root', Comment::create('comment'), Element::create('tag')->omitEndTag() );
Output (unformatted):
<?xml version = "1.0" encoding = "UTF-8" standalone = "yes" ?> <root> <!-- comment --> <tag /></root>
Compatibility
Details
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.