FluidXML is a PHP library, under the Servo PHP framework umbrella ☂,
designed to manipulate XML documents with a concise and fluent API.
It leverages XPath and the fluent programming pattern to be fun and effective.
STOP generating XML documents with template engines.
STOP using the boring and verbose DOMDocument.
FluidXML has been created to bring XML manipulation to the next level.
$book = new FluidXml();
$book->appendChild('title', 'The Theory Of Everything')
->appendChild('author', 'S. Hawking')
->appendChild('chapters', true)
->appendChild('chapter', 'Ideas About The Universe', ['id' => 1])
->appendChild('chapter', 'The Expanding Universe', ['id' => 2])
->query('//chapter')
->setAttribute('lang', 'en');
Or, if you prefer, there is a concise syntax.
$book = fluidxml();
$book->add('title', 'The Theory Of Everything')
->add('author', 'S. Hawking')
->add('chapters', true)
->add('chapter', 'Ideas About The Universe', ['id' => 1])
->add('chapter', 'The Expanding Universe', ['id' => 2])
->query('//chapter')
->attr('lang', 'en');
echo $book->xml();
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<title>The Theory Of Everything</title>
<author>S. Hawking</author>
<chapters>
<chapter id="1" lang="en">Ideas About The Universe</chapter>
<chapter id="2" lang="en">The Expanding Universe</chapter>
</chapters>
</doc>
Creating structured documents is so easy that you'll not believe.
$food = fluidxml();
// Batch insertion of nodes.
$food->add([ 'cake' => 'Tiramisu',
'pizza' => 'Margherita' ]);
// A bunch of egg's all with the same attribute.
$food->add([ ['egg'],
['egg'],
['egg'] ], ['price' => '0.25']);
// Deep tree structures are supported too.
$food->add([ 'fridge' => [ 'omelette' => 'with potato',
'soupe' => 'with mashrooms' ],
'freezer' => [ 'meat' => 'beef' ] ]);
XPath is king.
$book->query('//chapter')
->attr('status', 'read')
->query('..')
->attr('lang', 'en')
->query('../title')
->attr('country', 'us');
And sometimes string templates are the fastest way.
$book->appendChild('cover', true)
->appendXml(<<<XML
<h1>The Theory Of Everything</h1>
<img src="http://goo.gl/kO3Iov"/>
XML
);
XML Namespaces are fully covered too.
$book->namespace('xhtml', 'http://www.w3.org/1999/xhtml')
->namespace('svg', 'http://www.w3.org/2000/svg')
->appendChild('xhtml:h1')
->appendChild('svg:shape')
->query('//xhtml:h1');
Existing DOMDocument and SimpleXML documents are not a problem, just import them.
$fluidxml = fluidify($domdocument);
Don't be shy and tell it: « IT'S AWESOME! » ^_^
Other three great reasons to use FluidXML, but you'll have the best answer trying it yourself.
FluidXML is fun to use, concise and effective.
If it's not enough, it has a comprehensive test suite with a 100% code coverage.
- PHP 5.6
- Cloning the repository:
git clone https://github.com/servo-php/fluidxml.git
* **Using Composer**:
```sh
composer require servo/fluidxml
- Cloning the repository:
require_once 'FluidXml.php';
* **Using Composer**:
```php
require_once 'vendor/autoload.php';
See the documentation to get started and become a ninja.
10 minutes reading
Follow the Getting Started tutorial to become a ninja in no time.
Many other examples are available:
- inside the
documents/Examples.php
file - inside the
specs/FluidXml.php
file (as test cases)
All them cover from the simplest case to the most complex scenario.
Take a look at the APIs to discover all the available manipulation operations,
and go to the Wiki Page for more reading.
If you think this project is awesome or if you want to demonstrate
your immense gratitude ♡, donate 1cent.
Thank You! :D ♥
- [+] Porting the XML namespace implementation from the legacy FluidXML codebase
- Expanding the APIs
- [+] PHP 5.6 backport
- Extending the documentation
Daniele Orlando <fluidxml@danieleorlando.com>
FluidXML is licensed under the BSD 2-Clause License.
See documents/License.txt
for the details.