Repository navigation
Getting Started
FluidXML has been designed to give his best creating and manipulating XML documents.
It's quite common to see inside PHP projects the generation of XML documents through string template systems, PHP/XML mixing or, in the best case, using DOMDocument.
In every described situations FluidXML performs better in every way.
First of all, depending the way you have chosen to install FluidXML, you have two ways to include the library.
- If you have cloned the repository, copy the
source/FluidXml.phpin your PHP project and include it:
require_once 'FluidXml.php';
* If you have installed the library using Composer, include the autoloader:
```php
require_once 'vendor/autoload.php';
We can proceed to create our first XML document in the simplest and most common way it to create it.
Extended syntax
$book = new FluidXml();Concise syntax
$book = fluidxml();It creates a new XML document with one root node that by default is called <doc/>.
Whether there is the need to influence the document creation, the constructor supports many options.
Extended syntax
$book = new FluidXml(['root' => 'book']);Concise syntax
$book = fluidxml(['root' => 'book']);Our XML document now has a root node called <book/>.
Pro Tip: The constructor supports these other options:
[ 'version' => '1.0', // The version for the XML header.
'encoding' => 'UTF-8', // The encoding for the XML header.
'stylesheet' => null ] // An url pointing to an XSL file.
## Adding nodes
Adding a node is super easy. Because FluidXML implements the fluid OOP pattern, multiple operations can be performed chaining method calls.
> Extended syntax
```php
$book->appendChild('title', 'The Theory Of Everything')
->appendChild('author', 'S. Hawking')
->appendChild('description');Concise syntax
$book->add('title', 'The Theory Of Everything')
->add('author', 'S. Hawking')
->add('description');Our book now has three children nodes.
To view the XML string representation of our document we can output its content.
echo $book->xml();The appendChild/add method supports up to four arguments to achieve from the simplest node insertion to nested trees creation.
Public API:
->appendChild($node, $value?, $attributes? = [], $switchContext? = false)Pro Tip: Except for the
$nodeargument, all the others can be passed in any order.
One of the most important argument is the boolean flag $switchContext. Passing a true boolean value returns the new node instead of current node.
Extended syntax
// true asks to return the 'chapters' node instead of the 'book' node.
$book->appendChild('chapters', true)
->appendChild('chapter', 'Ideas About The Universe')
->appendChild('chapter', 'The Expanding Universe');Concise syntax
// true asks to return the 'chapters' node instead of the 'book' node.
$book->add('chapters', true)
->add('chapter', 'Ideas About The Universe')
->add('chapter', 'The Expanding Universe');The $chapters
Sometimes it's convenient to use an array as representation of our nodeName/nodeValue combination.
Extended syntax
$book->appendChild('title', 'The Theory Of Everything')
->appendChild('author', 'S. Hawking' );Concise syntax
$book->add('title', 'The Theory Of Everything')
->add('author', 'S. Hawking' );