Skip to content

edwardsteven/fluidxml

 
 

Repository files navigation

Travis Build Coveralls Coverage Scrutinizer Quality Code Climate Quality

Packagist License Packagist Last Release Packagist Total Downloads

Changelog

1.20.2 (2016-03-04): fixes some leaked PHP notices.

...

The full changes list.


Donate
1$ or more, due to the PayPal fees.

FluidXML

Servo-PHP Logo

      

FluidXML Logo

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

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

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

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"?>
<doc>
  <title>The Theory Of Everything</title>
  <author>S. Hawking</author>
  <chapters>
    <chapter id="1">Ideas About The Universe</chapter>
    <chapter id="2">The Expanding Universe</chapter>
  </chapters>
</doc>

XPath is king.

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

And CSS Selectors rocks.

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

XML/CSS Namespaces are fully covered.

$book->namespace('xhtml', 'http://www.w3.org/1999/xhtml')
     ->add('xhtml:h1')
     ->query('//xhtml:h1')  // XPath namespace.
     ->query('xhtml|h1');   // CSS namespace.

And sometimes string fragments are the fastest way.

$book->add(<<<XML
    <cover class="front">
        <img src="http://goo.gl/kO3Iov"/>
    </cover>
    <cover class="back">
        <img src="http://goo.gl/kO3Iov"/>
    </cover>
XML
);

Everything is fluent, even iterations.

$book->query('//chapter')
        ->each(function($i) {
             $this->attr('id', $i);
        });
$book->query('//chapters')
        ->times(3)
            ->add('chapter')
        ->times(4, function($i) {
            $this->add('chapter');
            $this->add('illustration');
        });

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

$book->query('//chapters')
        ->filter(function($i, $node) {
            return $i % 2 === 0;
        })
        ->attr('even');

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

fluidxml($domdocument)
    ->query('/html/body')
         ->add($simplexml);

// 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 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

Requirements

  • PHP 5.6

Installation

  • Cloning the repository:

git clone https://github.com/servo-php/fluidxml.git


* **Using Composer**:
  ```sh
composer require servo/fluidxml

Getting Started

  • Cloning the repository:

require_once 'FluidXml.php';


* **Using Composer**:
  ```php
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.

Documentation

10 minutes reading
Follow the Getting Started tutorial to become a ninja in no time.

Many other examples are available:

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.

Donation

If you think this code is awesome or if you want to demonstrate
your immense gratitude , donate 1cent.

Donate
1$ or more, due to the PayPal fees.

Roadmap

  • PHP 5.6 backport
  • Extending the documentation
  • Expanding the APIs
Click here to lend your support to: FluidXML and make a donation at pledgie.com !

Author

Daniele Orlando <fluidxml@danieleorlando.com>

License

FluidXML is licensed under the BSD 2-Clause License.

See documents/License.txt for the details.

About

FluidXML, the PHP library for manipulating XML with a concise and fluent API.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 96.9%
  • Shell 3.0%
  • Vim Script 0.1%