ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f87
XMLParser.php
Go to the documentation of this file.
1 <?php
2 
3 namespace SimpleExcel\Parser;
4 
6 
13 class XMLParser extends BaseParser implements IParser
14 {
21  protected $file_extension = 'xml';
22 
30  private function getAttributes($attrs_obj) {
31  $attrs_arr = array();
32  foreach ($attrs_obj as $attrs) {
33  $attrs = (array) $attrs;
34  foreach ($attrs as $attr) {
35  $attr_keys = array_keys($attr);
36  $attrs_arr[$attr_keys[0]] = $attr[$attr_keys[0]];
37  }
38  }
39  return $attrs_arr;
40  }
41 
51  public function getCell($row_num, $col_num, $val_only = true) {
52  // check whether the cell exists
53  if (!$this->isCellExists($row_num, $col_num)) {
54  throw new \Exception('Cell '.$row_num.','.$col_num.' doesn\'t exist', SimpleExcelException::CELL_NOT_FOUND);
55  }
56  if(is_array($this->table_arr['table_contents'][$row_num-1]['row_contents'])){
57  if(array_key_exists($col_num-1, $this->table_arr['table_contents'][$row_num-1]['row_contents'])){
58  $cell = $this->table_arr['table_contents'][$row_num-1]['row_contents'][$col_num-1];
59  if(!$val_only){
60  return $cell;
61  } else {
62  return $cell['value'];
63  }
64  }
65  }
66  return "";
67  }
68 
77  public function getColumn($col_num, $val_only = TRUE) {
78  $col_arr = array();
79 
80  if (!$this->isColumnExists($col_num)) {
81  throw new \Exception('Column '.$col_num.' doesn\'t exist', SimpleExcelException::COLUMN_NOT_FOUND);
82  }
83 
84  // get the specified column within every row
85  foreach ($this->table_arr['table_contents'] as $row) {
86  if ($row['row_contents']) {
87  if(!$val_only) {
88  array_push($col_arr, $row['row_contents'][$col_num-1]);
89  } else {
90  array_push($col_arr, $row['row_contents'][$col_num-1]['value']);
91  }
92  } else {
93  array_push($col_arr, "");
94  }
95  }
96 
97  // return the array
98  return $col_arr;
99  }
100 
108  public function getField($val_only = TRUE) {
109  if (!$this->isFieldExists()) {
110  throw new \Exception('Field is not set', SimpleExcelException::FIELD_NOT_FOUND);
111  }
112  if($val_only){
113  $field = array();
114  foreach($this->table_arr['table_contents'] as $row){
115  $cells = array();
116  if($row['row_contents']){
117  foreach($row['row_contents'] as $cell){
118  array_push($cells, $cell['value']);
119  }
120  }
121  array_push($field, $cells);
122  }
123  return $field;
124  } else {
125  return $this->table_arr;
126  }
127  }
128 
137  public function getRow($row_num, $val_only = TRUE) {
138  if (!$this->isRowExists($row_num)) {
139  throw new \Exception('Row '.$row_num.' doesn\'t exist', SimpleExcelException::ROW_NOT_FOUND);
140  }
141  $row = $this->table_arr['table_contents'][$row_num-1]['row_contents'];
142  $row_arr = array();
143 
144  // get the specified column within every row
145  foreach ($row as $cell) {
146  if (!$val_only) {
147  array_push($row_arr, $cell);
148  } else {
149  array_push($row_arr, $cell['value']);
150  }
151  }
152 
153  // return the array, if empty then return FALSE
154  return $row_arr;
155  }
156 
163  public function isColumnExists($col_num){
164  $exist = false;
165  foreach($this->table_arr['table_contents'] as $row){
166  if(is_array($row['row_contents'])){
167  if(array_key_exists($col_num-1, $row['row_contents'])){
168  $exist = true;
169  }
170  }
171  }
172  return $exist;
173  }
174 
181  public function isRowExists($row_num){
182  return array_key_exists($row_num-1, $this->table_arr['table_contents']);
183  }
184 
192  private function parseDOM($xml){
193 
194  // get XML namespace
195  $xmlns = $xml->getDocNamespaces();
196 
197  // check file extension and XML namespace
198  if ($xmlns['ss'] != 'urn:schemas-microsoft-com:office:spreadsheet') {
199  throw new \Exception('Document namespace isn\'t a valid Excel XML 2003 Spreadsheet', SimpleExcelException::INVALID_DOCUMENT_NAMESPACE);
200  }
201 
202  // extract document properties
203  $doc_props = (array)$xml->DocumentProperties;
204  $this->table_arr['doc_props'] = $doc_props;
205 
206  $rows = $xml->Worksheet->Table->Row;
207  $row_num = 1;
208  $this->table_arr = array(
209  'doc_props' => array(),
210  'table_contents' => array()
211  );
212 
213  // loop through all rows
214  foreach ($rows as $row) {
215 
216  // check whether ss:Index attribute exist in this row
217  $row_index = $row->xpath('@ss:Index');
218 
219  // if exist, push empty value until the specified index
220  if (count($row_index) > 0) {
221  $gap = $row_index[0]-count($this->table_arr['table_contents']);
222  for($i = 1; $i < $gap; $i++){
223  array_push($this->table_arr['table_contents'], array(
224  'row_num' => $row_num,
225  'row_contents' => '',
226  //'row_attrs' => $row_attrs_arr
227  ));
228  $row_num += 1;
229  }
230  }
231 
232  $cells = $row->Cell;
233  $row_attrs = $row->xpath('@ss:*');
234  $row_attrs_arr = $this->getAttributes($row_attrs);
235  $row_arr = array();
236  $col_num = 1;
237 
238  // loop through all row's cells
239  foreach ($cells as $cell) {
240 
241  // check whether ss:Index attribute exist
242  $cell_index = $cell->xpath('@ss:Index');
243 
244  // if exist, push empty value until the specified index
245  if (count($cell_index) > 0) {
246  $gap = $cell_index[0]-count($row_arr);
247  for ($i = 1; $i < $gap; $i++) {
248  array_push ($row_arr, array(
249  'row_num' => $row_num,
250  'col_num' => $col_num,
251  'datatype' => '',
252  'value' => '',
253  //'cell_attrs' => '',
254  //'data_attrs' => ''
255  ));
256  $col_num += 1;
257  }
258  }
259 
260  // get all cell and data attributes
261  //$cell_attrs = $cell->xpath('@ss:*');
262  //$cell_attrs_arr = $this->getAttributes($cell_attrs);
263  $data_attrs = $cell->Data->xpath('@ss:*');
264  $data_attrs_arr = $this->getAttributes($data_attrs);
265  $cell_datatype = $data_attrs_arr['Type'];
266 
267  // extract data from cell
268  $cell_value = (string) $cell->Data;
269 
270  // escape input from HTML tags
271  $cell_value = filter_var($cell_value, FILTER_SANITIZE_SPECIAL_CHARS);
272 
273  // push column array
274  array_push($row_arr, array(
275  'row_num' => $row_num,
276  'col_num' => $col_num,
277  'datatype' => $cell_datatype,
278  'value' => $cell_value,
279  //'cell_attrs' => $cell_attrs_arr,
280  //'data_attrs' => $data_attrs_arr
281  ));
282  $col_num += 1;
283  }
284 
285  // push row array
286  array_push($this->table_arr['table_contents'], array(
287  'row_num' => $row_num,
288  'row_contents' => $row_arr,
289  //'row_attrs' => $row_attrs_arr
290  ));
291  $row_num += 1;
292  }
293 
294  return true;
295  }
296 
303  public function loadFile($file_path) {
304 
305  if (!$this->isFileReady($file_path)) {
306  return false;
307  }
308 
309  return $this->parseDOM(simplexml_load_file($file_path));
310  }
311 
318  public function loadString($str){
319  return $this->parseDOM(simplexml_load_string($str));
320  }
321 }
getColumn($col_num, $val_only=TRUE)
Get data of the specified column as an array.
Definition: XMLParser.php:77
getRow($row_num, $val_only=TRUE)
Get data of the specified row as an array.
Definition: XMLParser.php:137
isRowExists($row_num)
Check whether a specified row exists.
Definition: XMLParser.php:181
define parser interface
Definition: IParser.php:13
isFileReady($file_path)
Check whether file exists, valid, and readable.
Definition: BaseParser.php:169
isCellExists($row_num, $col_num)
Check whether cell with specified row & column exists.
Definition: BaseParser.php:121
getCell($row_num, $col_num, $val_only=true)
Get value of the specified cell.
Definition: XMLParser.php:51
getField($val_only=TRUE)
Get data of all cells as an array.
Definition: XMLParser.php:108
isFieldExists()
Check whether table exists.
Definition: BaseParser.php:156
getAttributes($attrs_obj)
Extract attributes from SimpleXMLElement object.
Definition: XMLParser.php:30
loadString($str)
Load the string to be parsed.
Definition: XMLParser.php:318
parseDOM($xml)
Process the loaded file/string.
Definition: XMLParser.php:192
loadFile($file_path)
Load the XML file to be parsed.
Definition: XMLParser.php:303
isColumnExists($col_num)
Check whether a specified column exists.
Definition: XMLParser.php:163