PHP Classes

File: documents/

Recommend this page to a friend!
  Classes of Daniele Orlando   FluidXML   documents/   Download  
File: documents/
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: FluidXML
Manipulate XML documents using a fluent interface
Author: By
Last change: Fix markdown in installation and getting started sections
Date: 4 years ago
Size: 10,331 bytes


Class file image Download

[apis]: [gettingstarted]: [examples]: [specs]: [wiki]: [bsd]: [license]: [changelog]: [codecoverage]: [donate-button]: [donate-link]: [donate-link-alt]: [ninja]: [thankyou]:ón.svg/2000px-Heart_corazón.svg.png

[packagist]: [travis]: [scrutinizer]: [coveralls]: [codeclimate]: [codeship]: [circle]:

[packagist-license-badge]: [packagist-release-badge]: [packagist-downloads-badge]: [packagist-license-badge-alt]: [packagist-release-badge-alt]: [packagist-downloads-badge-alt]: [travis-build-badge]: [scrutinizer-coverage-badge]: [scrutinizer-quality-badge]: [coveralls-coverage-badge]: [codeclimate-quality-badge]: [codeship-build-badge]: [circle-build-badge]:

[![Travis Build][travis-build-badge]][travis] [![Coveralls Coverage][coveralls-coverage-badge]][coveralls] [![Scrutinizer Quality][scrutinizer-quality-badge]][scrutinizer] [![Code Climate Quality][codeclimate-quality-badge]][codeclimate]

[![Packagist License][packagist-license-badge]][packagist] [![Packagist Last Release][packagist-release-badge]][packagist] [![Packagist Total Downloads][packagist-downloads-badge]][packagist]


1.20.3 (2016-07-12): _fixes wrong handling of null/empty node value._


[The full changes list.][changelog]


<a href='' target='_blank'> <img height='32' src='' border='0' alt='Buy Me a Coffee at'/> </a>


<img src="" height="64px" alt="Servo-PHP Logo"/> <span>      </span> <img src="" height="64px" alt="FluidXML Logo"/>

FluidXML is a PHP library designed to manipulate XML documents with a concise and fluent API.<br/> It leverages the fluent programming pattern to be fun and effective.

$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]);

Or, if you prefer, there is an extended syntax.

$book = new FluidXml();

$book->addChild('title', 'The Theory Of Everything')
     ->addChild('author', 'S. Hawking')
     ->addChild('chapters', true)
         ->addChild('chapter', 'Ideas About The Universe', ['id' => 1])
         ->addChild('chapter', 'The Expanding Universe',   ['id' => 2]);

With FluidXML the DOM manipulation becomes fast, clear and expressive.

PHP Arrays are first class citizens.

$book->add([ 'title'  => 'The Theory Of Everything',
             'author' => 'S. Hawking',
             'chapters' => [
                    [ 'chapter' => [
                            '@id' => '1',
                            '@'   => 'Ideas About The Universe' ] ],
                    [ 'chapter' => [
                            '@id' => '2',
                            '@'   => 'The Expanding Universe' ] ],

echo $book;

<?xml version="1.0" encoding="UTF-8"?>
  <title>The Theory Of Everything</title>
  <author>S. Hawking</author>
    <chapter id="1">Ideas About The Universe</chapter>
    <chapter id="2">The Expanding Universe</chapter>

XPath is king.

$book->query('//title', '//author', '//chapter')
        ->attr('lang', 'en');

And CSS Selectors rock.

$book->query('#id', '.class1.class2', 'div p > span')
        ->attr('lang', 'en');

// Many other selectors are available.

XML/CSS Namespaces are fully covered.

$book->namespace('xhtml', '')
     ->query('//xhtml:h1')  // XPath namespace.
     ->query('xhtml|h1');   // CSS namespace.

And sometimes XML Fragments are the fastest way.

    <cover class="front">
        <img src=""/>
    <cover class="back">
        <img src=""/>

Everything is fluent, even iterations.

        ->each(function ($i) {
             $this->attr('id', $i);

        ->times(4, function ($i) {

Whether some queries are too complex to express with XPath/CSS,<br/> filtering is your friend.

        ->filter(function ($i, $node) {
            return $i % 2 === 0;

Interoperability with existing DOMDocument and SimpleXML is simply magic.<br/> Import them or inject them in any point of the FluidXML flow just like that.


// Yes, we merged a DOMDocument with a SimpleXMLElement
// and everything is still fluid.

Don't be shy and tell it: « IT'S AWESOME! » ^\_^

Many other [APIs][apis] are available: - __invoke() - append()/appendSibling() - prepend()/prependSibling() - addText() - text()/setText() - addCdata() - cdata()/setCdata() - addComment() - comment()/setComment() - remove() - size()/length() - load() - save() - dom() - xml() - html() - __toString() - array() - ...

Still doubts?

FluidXML is fun to use, concise and effective.

If it's not enough, it has a comprehensive test suite with a 100% code coverage.

But you'll have the best answer trying it yourself.

![100% Code Coverage][codecoverage]


  • PHP 5.6


  • Cloning the repository:
git clone

  • Using Composer:
composer require servo/fluidxml

Getting Started

  • Cloning the repository:
require_once 'FluidXml.php';

  • Using Composer:
require_once 'vendor/autoload.php';

use classes and functions as you need.

use function \FluidXml\fluidxml;
use function \FluidXml\fluidns;
use function \FluidXml\fluidify;
use \FluidXml\FluidXml;
use \FluidXml\FluidNamespace;

See the documentation to get started and become a [ninja][ninja].


_10 minutes reading_<br/> Follow the [Getting Started tutorial][gettingstarted] to become a [ninja][ninja] in no time.

Many other examples are available: - inside the [documents/Examples/][examples] folder - inside the [specs/FluidXml.php][specs] file (as test cases)

All them cover from the simplest case to the most complex scenario.

Take a look at the [APIs][apis] to discover all the available manipulation operations,<br/> and go to the [Wiki Page][wiki] for more reading.


If you think this code is awesome or if you want to demonstrate<br/> your immense gratitude [?][thankyou], _buy me a coffe_.

<a href='' target='_blank'>

<img height='32' src='' border='0' alt='Buy Me a Coffee at'/>


[//]: # ([![Donate][donate-button]][donate-link]<br/>) [//]: # (1$ or more<span style="color: gray;">, due to the PayPal fees.</span>)

<a-off href=''>

<img-off alt='Click here to lend your support to: FluidXML and make a donation at !' src='' border='0'/>



  • [x] PHP 5.6 backport
  • [ ] Extending the documentation
  • [ ] Expanding the APIs


Daniele Orlando &lt;;


FluidXML is licensed under the [BSD 2-Clause License][bsd].

See [documents/License.txt][license] for the details.