Repository navigation
Expand file tree
/
Copy pathXMLParser.php
More file actions
100 lines (96 loc) · 3.71 KB
/
Copy pathXMLParser.php
File metadata and controls
100 lines (96 loc) · 3.71 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
<?php
namespace Faisalman\SimpleExcel\Parser;
use Faisalman\SimpleExcel\Enums\Datatype;
use Faisalman\SimpleExcel\Enums\SimpleExcelException;
use Faisalman\SimpleExcel\Spreadsheet\Cell;
use Faisalman\SimpleExcel\Spreadsheet\Workbook;
use Faisalman\SimpleExcel\Spreadsheet\Worksheet;
/**
* SimpleExcel class for parsing Microsoft Excel 2003 XML Spreadsheet
*
* @author Faisal Salman
* @package Faisalman\SimpleExcel
*/
class XMLParser extends BaseParser
{
/**
* Defines valid file extension
*
* @access protected
* @var string
*/
protected $file_extension = 'xml';
/**
* Load the string to be parsed
*
* @param string $str String with XML format
* @param array $options Options
* @throws Exception If document namespace invalid
* @return bool
*/
public function loadString ($str, $options = NULL) {
$xml = simplexml_load_string($str);
$this->workbook = new Workbook();
$xmlns = $xml->getDocNamespaces();
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);
}
foreach($xml->Worksheet as $worksheet) {
$sheet = new Worksheet();
$col_max = 0;
foreach ($worksheet->Table->Row as $row) {
$record = array();
$row_index = $row->xpath('@ss:Index');
if (count($row_index) > 0) {
$gap = $row_index[0] - count($sheet->getRecords());
for($i = 1; $i < $gap; $i++){
$sheet->insertRecord(array());
}
}
$record = array();
$col_num = 0;
foreach ($row->Cell as $cell) {
$cell_index = $cell->xpath('@ss:Index');
if (count($cell_index) > 0) {
$gap = $cell_index[0] - count($record);
for ($i = 1; $i < $gap; $i++) {
array_push($record, new Cell(''));
}
}
$data_attrs = $cell->Data->xpath('@ss:*');
$cell_datatype = $data_attrs['Type'];
switch ($cell_datatype) {
case 'Number':
$cell_datatype = Datatype::NUMBER;
break;
case 'DateTime':
$cell_datatype = Datatype::DATETIME;
break;
case 'Error':
$cell_datatype = Datatype::ERROR;
break;
case 'String':
default:
$cell_datatype = Datatype::TEXT;
}
$cell_value = (string) $cell->Data;
array_push($record, new Cell($cell_value, $cell_datatype));
$col_num += 1;
}
if ($col_num > $col_max) {
$col_max = $col_num;
}
$sheet->insertRecord($record);
}
foreach ($sheet->getRecords() as $i => $record) {
if (($diff = $col_max - count($record)) > 0) {
$record = $sheet->getRecord($i + 1);
for ($j = 0; $j < $diff; $j++) {
array_push($record, new Cell(''));
}
}
}
$this->workbook->insertWorksheet($sheet);
}
}
}