ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
SimpleExcel\Parser\XMLParser Class Reference
+ Inheritance diagram for SimpleExcel\Parser\XMLParser:
+ Collaboration diagram for SimpleExcel\Parser\XMLParser:

Public Member Functions

 getCell ($row_num, $col_num, $val_only=true)
 Get value of the specified cell. More...
 
 getColumn ($col_num, $val_only=TRUE)
 Get data of the specified column as an array. More...
 
 getField ($val_only=TRUE)
 Get data of all cells as an array. More...
 
 getRow ($row_num, $val_only=TRUE)
 Get data of the specified row as an array. More...
 
 isColumnExists ($col_num)
 Check whether a specified column exists. More...
 
 isRowExists ($row_num)
 Check whether a specified row exists. More...
 
 loadFile ($file_path)
 Load the XML file to be parsed. More...
 
 loadString ($str)
 Load the string to be parsed. More...
 
- Public Member Functions inherited from SimpleExcel\Parser\BaseParser
 __construct ($file_url=NULL)
 
 getCell ($row_num, $col_num, $val_only=true)
 Get value of the specified cell. More...
 
 getColumn ($col_num, $val_only=TRUE)
 Get data of the specified column as an array. More...
 
 getField ($val_only=TRUE)
 Get data of all cells as an array. More...
 
 getRow ($row_num, $val_only=TRUE)
 Get data of the specified row as an array. More...
 
 isCellExists ($row_num, $col_num)
 Check whether cell with specified row & column exists. More...
 
 isColumnExists ($col_num)
 Check whether a specified column exists. More...
 
 isRowExists ($row_num)
 Check whether a specified row exists. More...
 
 isFieldExists ()
 Check whether table exists. More...
 
 isFileReady ($file_path)
 Check whether file exists, valid, and readable. More...
 

Protected Attributes

 $file_extension = 'xml'
 
- Protected Attributes inherited from SimpleExcel\Parser\BaseParser
 $table_arr
 
 $file_extension = ''
 

Private Member Functions

 getAttributes ($attrs_obj)
 Extract attributes from SimpleXMLElement object. More...
 
 parseDOM ($xml)
 Process the loaded file/string. More...
 

Detailed Description

Definition at line 13 of file XMLParser.php.

Member Function Documentation

◆ getAttributes()

SimpleExcel\Parser\XMLParser::getAttributes (   $attrs_obj)
private

Extract attributes from SimpleXMLElement object.

private

Parameters
object$attrs_obj
Returns
array

Definition at line 30 of file XMLParser.php.

Referenced by SimpleExcel\Parser\XMLParser\parseDOM().

30  {
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  }
+ Here is the caller graph for this function:

◆ getCell()

SimpleExcel\Parser\XMLParser::getCell (   $row_num,
  $col_num,
  $val_only = true 
)

Get value of the specified cell.

Parameters
int$row_numRow number
int$col_numColumn number
int$val_onlyWhether returns only it's value or complete data
Returns
array
Exceptions
ExceptionIf the cell identified doesn't exist.

Implements SimpleExcel\Parser\IParser.

Definition at line 51 of file XMLParser.php.

References SimpleExcel\Exception\SimpleExcelException\CELL_NOT_FOUND, and SimpleExcel\Parser\BaseParser\isCellExists().

51  {
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  }
isCellExists($row_num, $col_num)
Check whether cell with specified row & column exists.
Definition: BaseParser.php:121
+ Here is the call graph for this function:

◆ getColumn()

SimpleExcel\Parser\XMLParser::getColumn (   $col_num,
  $val_only = TRUE 
)

Get data of the specified column as an array.

Parameters
int$col_numColumn number
bool$val_onlyReturns (value only | complete data) for every cell, default to TRUE
Returns
array
Exceptions
ExceptionIf the column requested doesn't exist.

Implements SimpleExcel\Parser\IParser.

Definition at line 77 of file XMLParser.php.

References $row, SimpleExcel\Exception\SimpleExcelException\COLUMN_NOT_FOUND, and SimpleExcel\Parser\XMLParser\isColumnExists().

77  {
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  }
isColumnExists($col_num)
Check whether a specified column exists.
Definition: XMLParser.php:163
+ Here is the call graph for this function:

◆ getField()

SimpleExcel\Parser\XMLParser::getField (   $val_only = TRUE)

Get data of all cells as an array.

Parameters
bool$val_onlyReturns (value only | complete data) for every cell, default to TRUE
Returns
array
Exceptions
ExceptionIf the field is not set.

Implements SimpleExcel\Parser\IParser.

Definition at line 108 of file XMLParser.php.

References $row, SimpleExcel\Parser\BaseParser\$table_arr, SimpleExcel\Exception\SimpleExcelException\FIELD_NOT_FOUND, and SimpleExcel\Parser\BaseParser\isFieldExists().

108  {
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  }
isFieldExists()
Check whether table exists.
Definition: BaseParser.php:156
+ Here is the call graph for this function:

◆ getRow()

SimpleExcel\Parser\XMLParser::getRow (   $row_num,
  $val_only = TRUE 
)

Get data of the specified row as an array.

Parameters
int$row_numRow number
bool$val_onlyReturns (value only | complete data) for every cell, default to TRUE
Returns
array
Exceptions
ExceptionWhen a row is requested that doesn't exist.

Implements SimpleExcel\Parser\IParser.

Definition at line 137 of file XMLParser.php.

References $row, SimpleExcel\Parser\XMLParser\isRowExists(), and SimpleExcel\Exception\SimpleExcelException\ROW_NOT_FOUND.

137  {
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  }
isRowExists($row_num)
Check whether a specified row exists.
Definition: XMLParser.php:181
+ Here is the call graph for this function:

◆ isColumnExists()

SimpleExcel\Parser\XMLParser::isColumnExists (   $col_num)

Check whether a specified column exists.

Parameters
int$col_numColumn number
Returns
bool

Implements SimpleExcel\Parser\IParser.

Definition at line 163 of file XMLParser.php.

References $row.

Referenced by SimpleExcel\Parser\XMLParser\getColumn().

163  {
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  }
+ Here is the caller graph for this function:

◆ isRowExists()

SimpleExcel\Parser\XMLParser::isRowExists (   $row_num)

Check whether a specified row exists.

Parameters
int$row_numRow number
Returns
bool

Implements SimpleExcel\Parser\IParser.

Definition at line 181 of file XMLParser.php.

Referenced by SimpleExcel\Parser\XMLParser\getRow().

181  {
182  return array_key_exists($row_num-1, $this->table_arr['table_contents']);
183  }
+ Here is the caller graph for this function:

◆ loadFile()

SimpleExcel\Parser\XMLParser::loadFile (   $file_path)

Load the XML file to be parsed.

Parameters
string$file_pathPath to XML file
Returns
bool

Implements SimpleExcel\Parser\IParser.

Definition at line 303 of file XMLParser.php.

References SimpleExcel\Parser\BaseParser\isFileReady(), and SimpleExcel\Parser\XMLParser\parseDOM().

303  {
304 
305  if (!$this->isFileReady($file_path)) {
306  return false;
307  }
308 
309  return $this->parseDOM(simplexml_load_file($file_path));
310  }
isFileReady($file_path)
Check whether file exists, valid, and readable.
Definition: BaseParser.php:169
parseDOM($xml)
Process the loaded file/string.
Definition: XMLParser.php:192
+ Here is the call graph for this function:

◆ loadString()

SimpleExcel\Parser\XMLParser::loadString (   $str)

Load the string to be parsed.

Parameters
string$strString with XML format
Returns
bool

Implements SimpleExcel\Parser\IParser.

Definition at line 318 of file XMLParser.php.

References SimpleExcel\Parser\XMLParser\parseDOM().

318  {
319  return $this->parseDOM(simplexml_load_string($str));
320  }
parseDOM($xml)
Process the loaded file/string.
Definition: XMLParser.php:192
+ Here is the call graph for this function:

◆ parseDOM()

SimpleExcel\Parser\XMLParser::parseDOM (   $xml)
private

Process the loaded file/string.

Parameters
SimpleXMLElement$xmlSimpleXMLElement object of XML
Exceptions
ExceptionIf document namespace invalid
Returns
bool

Definition at line 192 of file XMLParser.php.

References $row, SimpleExcel\Parser\XMLParser\getAttributes(), and SimpleExcel\Exception\SimpleExcelException\INVALID_DOCUMENT_NAMESPACE.

Referenced by SimpleExcel\Parser\XMLParser\loadFile(), and SimpleExcel\Parser\XMLParser\loadString().

192  {
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  }
getAttributes($attrs_obj)
Extract attributes from SimpleXMLElement object.
Definition: XMLParser.php:30
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $file_extension

SimpleExcel\Parser\XMLParser::$file_extension = 'xml'
protected

Definition at line 21 of file XMLParser.php.


The documentation for this class was generated from the following file: