Repository navigation
Expand file tree
/
Copy pathXMLParser.php
More file actions
324 lines (287 loc) · 10.5 KB
/
Copy pathXMLParser.php
File metadata and controls
324 lines (287 loc) · 10.5 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
namespace SimpleExcel\Parser;
use SimpleExcel\Exception\SimpleExcelException;
/**
* SimpleExcel class for parsing Microsoft Excel 2003 XML Spreadsheet
*
* @author Faisalman
* @package SimpleExcel
*/
class XMLParser extends BaseParser implements IParser
{
/**
* Defines valid file extension
*
* @access protected
* @var string
*/
protected $file_extension = 'xml';
/**
* Extract attributes from SimpleXMLElement object
*
* @access private
* @param object $attrs_obj
* @return array
*/
private function getAttributes($attrs_obj) {
$attrs_arr = array();
if (!$attrs_obj) {
return $attrs_arr;
}
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
* @param int $val_only Whether returns only it's value or complete data
* @return array
* @throws Exception If the cell identified doesn't exist.
*/
public function getCell($row_num, $col_num, $val_only = true) {
// check whether the cell exists
if (!$this->isCellExists($row_num, $col_num)) {
throw new \Exception('Cell '.$row_num.','.$col_num.' doesn\'t exist', SimpleExcelException::CELL_NOT_FOUND);
}
if(is_array($this->table_arr['table_contents'][$row_num-1]['row_contents'])){
if(array_key_exists($col_num-1, $this->table_arr['table_contents'][$row_num-1]['row_contents'])){
$cell = $this->table_arr['table_contents'][$row_num-1]['row_contents'][$col_num-1];
if(!$val_only){
return $cell;
} else {
return $cell['value'];
}
}
}
return "";
}
/**
* 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 array
* @throws Exception If the column requested doesn't exist.
*/
public function getColumn($col_num, $val_only = TRUE) {
$col_arr = array();
if (!$this->isColumnExists($col_num)) {
throw new \Exception('Column '.$col_num.' doesn\'t exist', SimpleExcelException::COLUMN_NOT_FOUND);
}
// 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']);
}
} else {
array_push($col_arr, "");
}
}
// return the array
return $col_arr;
}
/**
* Get data of all cells as an array
*
* @param bool $val_only Returns (value only | complete data) for every cell, default to TRUE
* @return array
* @throws Exception If the field is not set.
*/
public function getField($val_only = TRUE) {
if (!$this->isFieldExists()) {
throw new \Exception('Field is not set', SimpleExcelException::FIELD_NOT_FOUND);
}
if($val_only){
$field = array();
foreach($this->table_arr['table_contents'] as $row){
$cells = array();
if($row['row_contents']){
foreach($row['row_contents'] as $cell){
array_push($cells, $cell['value']);
}
}
array_push($field, $cells);
}
return $field;
} else {
return $this->table_arr;
}
}
/**
* 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 array
* @throws Exception When a row is requested that doesn't exist.
*/
public function getRow($row_num, $val_only = TRUE) {
if (!$this->isRowExists($row_num)) {
throw new \Exception('Row '.$row_num.' doesn\'t exist', SimpleExcelException::ROW_NOT_FOUND);
}
$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;
}
/**
* Check whether a specified column exists
*
* @param int $col_num Column number
* @return bool
*/
public function isColumnExists($col_num){
$exist = false;
foreach($this->table_arr['table_contents'] as $row){
if(is_array($row['row_contents'])){
if(array_key_exists($col_num-1, $row['row_contents'])){
$exist = true;
}
}
}
return $exist;
}
/**
* Check whether a specified row exists
*
* @param int $row_num Row number
* @return bool
*/
public function isRowExists($row_num){
return array_key_exists($row_num-1, $this->table_arr['table_contents']);
}
/**
* Process the loaded file/string
*
* @param SimpleXMLElement $xml SimpleXMLElement object of XML
* @throws Exception If document namespace invalid
* @return bool
*/
private function parseDOM($xml){
// get XML namespace
$xmlns = $xml->getDocNamespaces();
// check file extension and XML namespace
if ($xmlns['ss'] != 'urn:schemas-microsoft-com:office:spreadsheet') {
throw new \Exception('Document namespace isn\'t a valid Excel XML 2003 Spreadsheet', SimpleExcelException::INVALID_DOCUMENT_NAMESPACE);
}
// extract document properties
$doc_props = (array)$xml->DocumentProperties;
$this->table_arr['doc_props'] = $doc_props;
$rows = $xml->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 = isset($data_attrs_arr['Type']) ? $data_attrs_arr['Type'] : 'String';
// 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;
}
return true;
}
/**
* Load the XML file to be parsed
*
* @param string $file_path Path to XML file
* @return bool
*/
public function loadFile($file_path) {
if (!$this->isFileReady($file_path)) {
return false;
}
return $this->parseDOM(simplexml_load_file($file_path));
}
/**
* Load the string to be parsed
*
* @param string $str String with XML format
* @return bool
*/
public function loadString($str){
return $this->parseDOM(simplexml_load_string($str));
}
}