Logo

Programming-Idioms

History of Idiom 104 > diff from v5 to v6

Edit summary for version 6 by :
New PHP implementation by user [agilla1]

Version 5

2016-02-18, 16:58:02

Version 6

2016-02-19, 15:02:00

Idiom #104 Save object into XML file

Write content of object x into file data.xml.

Idiom #104 Save object into XML file

Write content of object x into file data.xml.

Imports
require_once('XML/Serializer.php');
Code
class Test
{
    public $f1;
    private $f2;

    public function __construct($f1, $f2)
    {
        $this->f1 = $f1;
        $this->f2 = $f2;
    }
}

$x = new Test('foo', 42);

$options = array(
  XML_SERIALIZER_OPTION_INDENT        => '    ',
  XML_SERIALIZER_OPTION_RETURN_RESULT => true
  );

$serializer = new XML_Serializer($options);
$result = "<?xml version=\"1.0\"?>\n" . $serializer->serialize($x);
file_put_contents('test.xml', $result);
Comments bubble
Requires PEAR XML_Serializer
Doc URL
https://pear.php.net/package/XML_Serializer/docs/0.20.2/XML_Serializer/XML_Serializer.html
Origin
https://pear.php.net/manual/en/package.xml.xml-serializer.xml-serializer.examples.php