Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?PHP
2
/**
3
 * This example shows how XML_Serializer and XML_Unserializer
4
 * work together.
5
 *
6
 * A structure is serialized and later it's unserialized from the
7
 * resulting XML document.
8
 *
9
 * @author  Stephan Schmidt <schst@php.net>
10
 */
11
error_reporting(E_ALL);
12
 
13
require_once 'XML/Serializer.php';
14
require_once 'XML/Unserializer.php';
15
// this is just to get a nested object
16
$pearError = PEAR::raiseError('This is just an error object',123);
17
 
18
$options = array(
19
                    XML_SERIALIZER_OPTION_INDENT      => '    ',
20
                    XML_SERIALIZER_OPTION_LINEBREAKS  => "\n",
21
                    XML_SERIALIZER_OPTION_DEFAULT_TAG => 'unnamedItem',
22
                    XML_SERIALIZER_OPTION_TYPEHINTS   => true
23
                );
24
 
25
$foo = new stdClass();
26
$foo->value = 'My value';
27
$foo->error = $pearError;
28
$foo->xml   = array('This is' => 'cool');
29
$foo->resource = fopen(__FILE__, 'r');
30
 
31
$serializer = new XML_Serializer($options);
32
$result = $serializer->serialize($foo);
33
 
34
if ($result === true) {
35
    $xml = $serializer->getSerializedData();
36
}
37
 
38
echo '<pre>';
39
echo htmlspecialchars($xml);
40
echo '</pre>';
41
 
42
//  be careful to always use the ampersand in front of the new operator
43
$unserializer = &new XML_Unserializer();
44
 
45
$status = $unserializer->unserialize($xml);
46
 
47
if (PEAR::isError($status)) {
48
    echo 'Error: ' . $status->getMessage();
49
} else {
50
    $data = $unserializer->getUnserializedData();
51
    echo '<pre>';
52
    print_r($data);
53
    echo '</pre>';
54
}
55
?>