-
Notifications
You must be signed in to change notification settings - Fork 20
SimpleXML Migration
If you worked with SimpleXML before the following examples should help you to understand FluentDOM. Like SimpleXML, FluentDOM uses PHP language features and interfaces to provide an easier and more compact syntax. Unlike SimpleXML, FluentDOM tries not to hide DOM, but to extend it. PHP itself implements DOM Level 2, FluentDOM adds DOM Level 3 methods.
The complete source of the following examples can be found in the repository: /examples/SimpleXML Migration.
FluentDOM::load()
returns an extended DOM document.
$element = simplexml_load_string($string);
echo $element->saveXML();
$element = simplexml_load_file($file);
echo $element->saveXML();
$document = FluentDOM::load($string);
echo $document->saveXML();
$document = FluentDOM::load($file, 'xml', [FluentDOM\Loader\Options::IS_FILE => TRUE]);
echo $document->saveXML();
$document = FluentDOM::load('<div/>', 'html');
echo $document->saveHTML();
$document = FluentDOM::load('{"foo": "bar"}', 'json');
echo $document->saveXML();
In SimpleXML you can use object property syntax to access the tag structure. The property can be cast to string to fetch the direct text children content.
$element = simplexml_load_string($xml);
echo $element->channel->title, "\n";
FluentDOM uses Xpath to accomplish that. Nodes can be used like functions to execute an Xpath expression in the context of the node. Xpath has a string cast built in.
$document = FluentDOM::load($xml);
echo $document('string(/rss/channel/title)'), "\n";
- Home
- Getting Started
- Tasks
- Plugins
- Functions
- Lists
- Creator (5.1)
- CSS Selectors
- Convertors
- Loaders
- Serializers (5.1)
- Transformers (5.1)
- Extended DOM
- XMLReader (6.1)
- XMLWriter (6.1)
- Interfaces