-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathserializeWithTagMap.php
81 lines (68 loc) · 2.7 KB
/
serializeWithTagMap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?PHP
/**
* XML Serializer example
*
* This example demonstrates, how XML_Serializer is able
* to serialize scalar values as an attribute instead of a nested tag.
*
* In this example tags with more than one attribute become
* multiline tags, as each attribute gets written to a
* separate line as 'indentAttributes' is set to '_auto'.
*
* @author Stephan Schmidt <[email protected]>
*/
error_reporting(E_ALL);
require_once 'XML/Serializer.php';
$options = array(
'indent' => ' ',
'linebreak' => "\n",
'mode' => 'simplexml',
'rootName' => 'items'
);
$data = array(
'item' => array( array(
'title' => 'Foobar!',
'description' => 'This is some text....',
'link' => 'http://foobar.com'
),
array(
'title' => 'Foobar2!',
'description' => 'This is some text.ü ü ä ö',
'link' => 'http://foobar.com'
)
)
);
$serializer = new XML_Serializer($options);
$result = $serializer->serialize($data);
if( $result === true ) {
$xml = $serializer->getSerializedData();
echo '<pre>';
print_r( htmlspecialchars($xml) );
echo '</pre>';
} else {
echo '<pre>';
print_r($result);
echo '</pre>';
}
$newOptions = array(
'rootName' => 'body',
'replaceEntities' => XML_SERIALIZER_ENTITIES_HTML,
'tagMap' => array(
'item' => 'div',
'title' => 'h1',
'description' => 'p',
'link' => 'tt'
)
);
$result = $serializer->serialize($data, $newOptions);
if( $result === true ) {
$xml = $serializer->getSerializedData();
echo '<pre>';
print_r( htmlspecialchars($xml) );
echo '</pre>';
} else {
echo '<pre>';
print_r($result);
echo '</pre>';
}
?>