Download the PHP package damijanc/simple-xml without Composer
On this page you can find all versions of the php package damijanc/simple-xml. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download damijanc/simple-xml
More information about damijanc/simple-xml
Files in damijanc/simple-xml
Package simple-xml
Short Description DomDocument wrapper for XML generation
License MIT
Informations about the package simple-xml
simple-xml
Simple wrapper for PHP (http://www.php.net/manual/en/class.domdocument.php) DOMDocument
After roughly 11 years this lib is finally getting and update. It uses a nice PHP feature - properties :D
Something about XML itself
Valid XML needs to start with xml version and encoding declaration like . XML without it is invalid.
Library by default uses UTF-8 encoding. So please make sure that all your strings are properly encoded.
XML supports multiple type of text: text, CDATA and an element.
Everything in CDATA section will be interpreted as a literal string. For example
will be interpreted as , as literal string. To be compatible with XML Specs you can use CDATA if your sting contains chars like <, >, & etc.
Text attribute will make sure that same characters will be escaped. will be , will be and will become . Up to you what you want to use.
If nothing is selected value will be used as is. Maybe I will add some formatters later like date and currency as they are supported in PHP.
Installation
Usage
We need to define a class with attributes to tell the library what kind of XML we want to have. At the moment setters and getters are no supported, but I might add that later. If property is an array we expect to have array of classes that are similarly decorated with attributes
In the example we have a collection of books. For that we need to define a collection class books and a books class, as in the example below.
Outputs:
Note on comments: As comments can appear anywhere in the XML it is hard to predict the user's intention. I decided to add comment support on the node attribute level
for example:
will result in:
I might change this implementation later. For now, I do not have any real use case.
Supported attributes
Propery attribute will add a propery to a node. It accepts two values name and value. If value is null it will be read from class property. We also have a support for multiple properties attributes that can added to mutiple class properties. Example above will attach properties to parent node as Node attribute is not defined. In the case in the example we would get
Another example with Node attribute
will result in:
Property
A word about performance
I did manage to generate XML from 50k classes in 300ms, therefor I believe it is fast enough. Would be nice if someone can confirm it.
All versions of simple-xml with dependencies
php Version >=8.2
ext-mbstring Version *