chinabank_utf8.CreateXML.php
3.71 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
class CreateXML {
/**
* 编码
* @access private
* @var array1
*/
//var $encoding;
/**
* @access private
* @var array1
*/
var $_tree;
var $encoding="UTF-8";
/**
* Constructor
*/
function CBCreateXML($encoding = 'UTF-8') {
$this->encoding = $encoding;
$this->_tree = array();
}
/**
* 建立根节点
*/
function createRoot($sname, $sversion) {
$tree = array();
$tree['tag'] = 'cb-xdoc';
$tree['attrs'] = array(
'sname' => $sname,
'sversion' => $sversion
);
/*
$tree['att.sname'] = $sname;
$tree['att.sversion'] = $sversion;
*/
$this->_tree = $tree;
}
/**
* 建立普通节点
*/
function addNode($name, $type, $val) {
$node = array();
$node['tag'] = 'node';
$node['attrs'] = array(
'name' => $name,
'type' => $type
);
switch ($type) {
case 'list':
case 'map':
$node['node'] = array();
foreach($val as $k=>$v) {
$node['node'][] = $this->_recursionGetNode($k, $v);
}
break;
default:
$node['val'] = $val;
}
/*
$node['att.name'] = $name;
$node['att.type'] = $type;
if ($type = 'money') {
$node['att.currency'] = 'CNY';
}
$node['val'] = $val;
*/
$this->_tree['node'][] = $node;
}
function _recursionGetNode($name, $val) {
$node = array();
$node['tag'] = 'node';
$node['attrs'] = array('name' => $name);
$type = gettype($val);
switch ($type) {
case "array":
$node['attrs']['type'] = 'map';
foreach($val as $k=>$v) {
$node['node'][] = $this->_recursionGetNode($k, $v);
}
break;
case "integer":
$node['attrs']['type'] = 'int';
$node['val'] = $val;
break;
default:
$node['attrs']['type'] = 'string';
$node['val'] = $val;
}
return $node;
}
function getTree() {
return $this->_tree;
}
function getString() {
$tree = $this->_tree;
$strxml = "<?xml version=\"1.0\" encoding=\"{$this->encoding}\"?>\n";
$strxml .= "<{$tree['tag']} sname=\"{$tree['attrs']['sname']}\" sversion=\"{$tree['attrs']['sversion']}\">\n";
foreach ($tree['node'] as $node) {
$strxml .= $this->_2str($node);
}
$strxml .= "</{$tree['tag']}>\n";
return $strxml;
}
function _2str($node, $sp=" ") {
$tag = $node['tag'];
$name = $node['attrs']['name'];
$type = $node['attrs']['type'];
$val = $node['val'];
// 开始 <node>
if ($type == 'money') {
$str = "$sp<$tag name=\"$name\" type=\"$type\" currency=\"{$node['att.currency']}\">\n";
} else {
$str = "$sp<$tag name=\"$name\" type=\"$type\">\n";
}
/**
if ($type == 'string') {
$str .= '<![CDATA[' . $val . ']]>';
} else {
$str .= $val;
}*/
// 值
switch($type) {
case 'NULL':
return '';
case 'string':
$str .= "$sp <![CDATA[$val]]>\n";
break;
case 'list':
case 'map':
if (array_key_exists('node', $node)) {
foreach ($node['node'] as $n) {
$str .= $this->_2str($n, $sp." ");
}
}
break;
default:
$str .= "$sp $val\n";
}
// 关闭 </node>
$str .= "$sp</$tag>\n";
return $str;
}
}
?>