chinabank_utf8.XmlParser.php
4.94 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
function print_a($name, $arr) {
echo "$name: ";
print_r($arr);
echo "<br/>\n";
}
class SaxElement {
var $parentNode;
var $tagName;
var $val;
var $attrs;
var $childs;
function SaxElement($tagName) {
$this->tagName = $tagName;
$this->attrs = array();
$this->childs = array();
}
function setParentNode(& $parentNode) {
$this->parentNode = & $parentNode;
}
function setValue($val) {
$this->val = $val;
}
function addAttr($key, $val) {
$this->attrs[$key] = $val;
}
function setAttrs($attrs) {
$this->attrs = $attrs;
}
function addChild(& $element) {
array_push($this->childs, &$element);
}
function getParent() {
return $this->parent;
}
function getChilds() {
return $this->childs;
}
function isRoot() {
return $this->parent == NULL;
}
/**
* 得到类XML的结构
*/
function getArray() {
$arr = array();
$arr['tag'] = $this->tagName;
$arr['attrs'] = $this->attrs;
if ($this->val != NULL) {
$arr['value'] = $this->val;
} elseif ( sizeof($this->childs) ) {
$arr['childs'] = array();
foreach($this->childs as $childElement) {
$arr['childs'][] = $childElement->getArray();
}
}
return $arr;
}
/**
* 得到简化的结构
*/
function getArray2() {
$rs = array();
$rs['sname'] = $this->attrs['sname'];
$rs['result'] = $this->attrs['result'];
foreach($this->childs as $child) {
$rt = $child->_getArray2();
$rs[ $rt['key'] ] = $rt['value'];
}
return $rs;
}
function _getArray2() {
$rt = array();
$name = $this->attrs['name'];
$type = $this->attrs['type'];
switch ($type) {
case 'list':
case 'map':
$var = array();
foreach($this->childs as $i => $child) {
$_rt = $child->_getArray2();
$k = $_rt['key'];
$k = ($k==null ? $i : $k);
$var[$k] = $_rt['value'];
}
break;
default:
$var = $this->val;
}
$rt['key'] = $name;
$rt['value'] = $var;
return $rt;
}
}
$rootDom;
$stack = array();
function startElement($parser, $name, $attrs) {
global $rootDom, $stack;
if ( sizeof($stack)==0 ) {
// 当栈为空时认为是XML的根节点
$rootDom = new SaxElement($name);
$rootDom->setAttrs($attrs);
array_push($stack, &$rootDom);
} else {
$element = new SaxElement($name);
$element->setAttrs($attrs);
$parent = &$stack[sizeof($stack)-1];
$element->setParentNode($parent);
$parent->addChild($element);
array_push($stack, &$element);
}
}
function endElement($parser, $name) {
global $stack;
array_pop($stack);
}
function characterData($parser, $data) {
if ( trim($data)!="" ) {
global $stack;
$element = &$stack[sizeof($stack)-1];
$element->setValue($data);
}
}
function xml2array($xml) {
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
if (!xml_parse($xml_parser, $xml)) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
xml_parser_free($xml_parser);
global $rootDom;
return $rootDom->getArray2();
}
/*
* 测试
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<root sname=\"ConsumeService\" result=\"0\">
<node name=\"time\" type=\"date\">2008-01-22</node>
<node name=\"merchantid\" type=\"string\">1001</node>
<node name=\"ch\" type=\"list\">
<node type=\"int\">22</node>
<node type=\"int\">33</node>
<node type=\"string\">44</node>
</node>
<node name=\"mapping\" type=\"map\">
<node name=\"aaa\" type=\"string\">22</node>
<node name=\"bbb\" type=\"int\">33</node>
<node name=\"ccc\" type=\"int\">44</node>
</node>
</root>";
$arr = xml2array($xml);
print_a('return', $arr);
$xml = "<?xml version=\"1.0\" encoding=\"GBK\"?>
<cb-xdoc sname=\"ConsumeService\" result=\"E12042000\"><node name=\"_error\" type=\"string\"><![CDATA[银行不允许此交易]]></node><node name=\"error\" type=\"string\"><![CDATA[E12042000]]></node></cb-xdoc>";
$arr = xml2array($xml);
print_a('return', $arr);
*/
?>