Skip to content

SimpleXML Migration

Thomas Weinert edited this page Dec 21, 2016 · 8 revisions

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: [https://github.com/FluentDOM/FluentDOM/tree/master/examples/SimpleXML%20Migration].

Load

FluentDOM::load() returns an extended DOM document.

SimpleXML load xml from string

$element = simplexml_load_string($string);
echo $element->saveXML();

SimpleXML load xml from file

$element = simplexml_load_file($file);
echo $element->saveXML();

FluentDOM load xml from string

$document = FluentDOM::load($string);
echo $document->saveXML();

FluentDOM load xml from file

$document = FluentDOM::load($file, 'xml', [FluentDOM\Loader\Options::IS_FILE => TRUE]);
echo $document->saveXML();

FluentDOM load html from string

$document = FluentDOM::load('<div/>', 'html');
echo $document->saveHTML();

FluentDOM load json from string

$document = FluentDOM::load('{"foo": "bar"}', 'json');
echo $document->saveXML();
Clone this wiki locally