Repository navigation
Expand file tree
/
Copy pathXML.php
More file actions
278 lines (243 loc) · 7.58 KB
/
Copy pathXML.php
File metadata and controls
278 lines (243 loc) · 7.58 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/**
* SimpleExcel class for parsing Microsoft Excel 2003 XML Spreadsheet
*
* @author Faisalman
* @package SimpleExcel
*/
class SimpleExcel_Parser_XML implements SimpleExcel_Parser_Interface
{
/**
* Holds the parsed result
*
* @access private
* @var array
*/
private $table_arr;
/**
* @param string $file_url Path to XML file (optional)
* @return void
*/
public function __construct($file_url = NULL){
if(isset($file_url)) $this->loadFile($file_url);
}
/**
* Extract attributes from SimpleXMLElement object
*
* @access private
* @param object $attrs_obj
* @return array
*/
private function getAttributes($attrs_obj){
$attrs_arr = array();
foreach($attrs_obj as $attrs){
$attrs = (array)$attrs;
foreach($attrs as $attr){
$attr_keys = array_keys($attr);
$attrs_arr[$attr_keys[0]] = $attr[$attr_keys[0]];
}
}
return $attrs_arr;
}
/**
* Get value of the specified cell
*
* @param int $row_num Row number
* @param int $col_num Column number
* @return mixed Returns an array or FALSE if cell doesn't exist
*/
public function getCell($row_num, $col_num){
// check whether the cell exists
if(!isset($this->table_arr['table_contents'][$row_num-1]['row_contents'][$col_num-1])){
throw new Exception('Cell '.$row_num.','.$col_num.' doesn\'t exist');
return FALSE;
}
return $this->table_arr['table_contents'][$row_num-1]['row_contents'][$col_num-1]['value'];
}
/**
* Get datatype of the specified cell
*
* @param int $row_num Row number
* @param int $col_num Column number
* @return mixed Returns an array or FALSE if cell doesn't exist
*/
public function getCellDatatype($row_num, $col_num){
// check whether the cell exists
if(!isset($this->table_arr['table_contents'][$row_num-1]['row_contents'][$col_num-1])){
throw new Exception('Cell '.$row_num.','.$col_num.' doesn\'t exist');
return FALSE;
}
return $this->table_arr['table_contents'][$row_num-1]['row_contents'][$col_num-1]['datatype'];
}
/**
* Get data of the specified column as an array
*
* @param int $col_num Column number
* @param bool $val_only Returns (value only | complete data) for every cell, default to TRUE
* @return mixed Returns an array or FALSE if table doesn't exist
*/
public function getColumn($col_num, $val_only = TRUE){
$col_arr = array();
if(!isset($this->table_arr['table_contents'])){
throw new Exception('Column '.$col_num.' doesn\'t exist');
return FALSE;
}
// get the specified column within every row
foreach($this->table_arr['table_contents'] as $row){
if($row['row_contents']){
if(!$val_only){
array_push($col_arr,$row['row_contents'][$col_num-1]);
} else {
array_push($col_arr,$row['row_contents'][$col_num-1]['value']);
}
}
}
// return the array, if empty then return FALSE
return $col_arr;
}
/**
* Get data of all cells as an array
*
* @return mixed Returns an array or FALSE if table doesn't exist
*/
public function getField(){
if(isset($this->table_arr)){
return $this->table_arr;
} else {
throw new Exception('Field is empty');
return FALSE;
}
}
/**
* Get data of the specified row as an array
*
* @param int $row_num Row number
* @param bool $val_only Returns (value only | complete data) for every cell, default to TRUE
* @return mixed Returns an array FALSE if row doesn't exist
*/
public function getRow($row_num, $val_only = TRUE){
if(!isset($this->table_arr['table_contents'][$row_num-1]['row_contents'])){
throw new Exception('Row '.$row_num.' doesn\'t exist');
return FALSE;
}
$row = $this->table_arr['table_contents'][$row_num-1]['row_contents'];
$row_arr = array();
// get the specified column within every row
foreach($row as $cell){
if(!$val_only){
array_push($row_arr,$cell);
} else {
array_push($row_arr,$cell['value']);
}
}
// return the array, if empty then return FALSE
return $row_arr;
}
/**
* Load the XML file to be parsed
*
* @param string $file_url Path to XML file
* @param bool $escape Set whether input had to be escaped from HTML tags, default to TRUE
* @return bool Returns TRUE if file exist and valid, FALSE if does'nt
*/
public function loadFile($file_path){
$file_extension = strtoupper(pathinfo($file_path, PATHINFO_EXTENSION));
if(!file_exists($file_path)){
throw new Exception('File doesn\'t exist');
return FALSE;
} else if($file_extension != 'XML'){
throw new Exception('File isn\'t an Excel XML 2003 Spreadsheet');
return FALSE;
}
// assign simpleXML object
$simplexml_table = simplexml_load_file($file_path);
// get XML namespace
$xmlns = $simplexml_table->getDocNamespaces();
// check file extension and XML namespace
if($xmlns['ss'] != 'urn:schemas-microsoft-com:office:spreadsheet'){
throw new Exception('File isn\'t a valid Excel XML 2003 Spreadsheet');
return FALSE;
}
// extract document properties
$doc_props = (array)$simplexml_table->DocumentProperties;
$this->table_arr['doc_props'] = $doc_props;
$rows = $simplexml_table->Worksheet->Table->Row;
$row_num = 1;
$this->table_arr = array(
'doc_props' => array(),
'table_contents' => array()
);
// loop through all rows
foreach($rows as $row){
// check whether ss:Index attribute exist in this row
$row_index = $row->xpath('@ss:Index');
// if exist, push empty value until the specified index
if(count($row_index) > 0){
$gap = $row_index[0]-count($this->table_arr['table_contents']);
for($i = 1; $i < $gap; $i++){
array_push($this->table_arr['table_contents'], array(
'row_num' => $row_num,
'row_contents' => '',
//'row_attrs' => $row_attrs_arr
));
$row_num += 1;
}
}
$cells = $row->Cell;
$row_attrs = $row->xpath('@ss:*');
$row_attrs_arr = $this->getAttributes($row_attrs);
$row_arr = array();
$col_num = 1;
// loop through all row's cells
foreach($cells as $cell){
// check whether ss:Index attribute exist
$cell_index = $cell->xpath('@ss:Index');
// if exist, push empty value until the specified index
if(count($cell_index) > 0){
$gap = $cell_index[0]-count($row_arr);
for($i = 1; $i < $gap; $i++){
array_push($row_arr,array(
'row_num' => $row_num,
'col_num' => $col_num,
'datatype' => '',
'value' => '',
//'cell_attrs' => '',
//'data_attrs' => ''
));
$col_num += 1;
}
}
// get all cell and data attributes
//$cell_attrs = $cell->xpath('@ss:*');
//$cell_attrs_arr = $this->getAttributes($cell_attrs);
$data_attrs = $cell->Data->xpath('@ss:*');
$data_attrs_arr = $this->getAttributes($data_attrs);
$cell_datatype = $data_attrs_arr['Type'];
// extract data from cell
$cell_value = (string)$cell->Data;
// escape input from HTML tags
$cell_value = filter_var($cell_value, FILTER_SANITIZE_SPECIAL_CHARS);
// push column array
array_push($row_arr,array(
'row_num' => $row_num,
'col_num' => $col_num,
'datatype' => $cell_datatype,
'value' => $cell_value,
//'cell_attrs' => $cell_attrs_arr,
//'data_attrs' => $data_attrs_arr
));
$col_num += 1;
}
// push row array
array_push($this->table_arr['table_contents'],array(
'row_num' => $row_num,
'row_contents' => $row_arr,
//'row_attrs' => $row_attrs_arr
));
$row_num += 1;
}
// load succeed :)
return TRUE;
}
}
?>