AccesoDatosXML.php
8.7 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
<?php
namespace clases\accesoDatos;
/**
* Obtiene datos de un archivo XML.
*/
class AccesoDatosXML implements iAccesoDatos {
private $posicionActual;
private $handle;
public $totalBytes;
public $readBytes = 0;
private $nodeIndex = 0;
private $chunk = "";
private $chunkSize;
private $readFromChunkPos;
private $rootNode;
private $customRootNode;
private $procesando = false;
/**
*
* @param mixed $mixed Path to XML file OR file handle
* @param int $posicionActual
* @param int $chunkSize Bytes to read per cycle (Optional, default is 32 KiB)
* @param string $customRootNode Specific root node to use (Optional)
* @param int $totalBytes Xml file size - Required if supplied file handle
* @param string $customChildNode
* @throws Exception
*/
public function __construct($mixed, $posicionActual = 0, $chunkSize = 32768, $customRootNode = null, $totalBytes = null, $customChildNode = null) {
if (is_string($mixed)) {
$this->handle = fopen($mixed, "r");
if (isset($totalBytes)) {
$this->totalBytes = $totalBytes;
} else {
$this->totalBytes = filesize($mixed);
}
} else if (is_resource($mixed)) {
$this->handle = $mixed;
if (!isset($totalBytes)) {
throw new Exception("totalBytes parameter required when supplying a file handle.");
}
$this->totalBytes = $totalBytes;
}
$this->chunkSize = $chunkSize;
$this->customRootNode = $customRootNode;
$this->customChildNode = $customChildNode;
$this->posicionActual = $posicionActual;
}
/**
* Gets the total read bytes so far
*/
public function getReadBytes() {
return $this->readBytes;
}
/**
* Gets the total file size of the xml
*/
public function getTotalBytes() {
return $this->totalBytes;
}
/**
* Starts the streaming and parsing of the XML file
*/
public function obtenerFila() {
$elementWithChildren = null;
$continue = true;
while ($continue) {
if(!$this->procesando) {
$continue = $this->readNextChunk();
if (!isset($this->rootNode)) {
// Find root node
if (isset($this->customRootNode)) {
$customRootNodePos = strpos($this->chunk, "<{$this->customRootNode}");
if ($customRootNodePos !== false) {
// Found custom root node
// Support attributes
$closer = strpos(substr($this->chunk, $customRootNodePos), ">");
$readFromChunkPos = $customRootNodePos + $closer + 1;
// Custom child node?
if (isset($this->customChildNode)) {
// Find it in the chunk
$customChildNodePos = strpos(substr($this->chunk, $readFromChunkPos), "<{$this->customChildNode}");
if ($customChildNodePos !== false) {
// Found it!
$readFromChunkPos = $readFromChunkPos + $customChildNodePos;
} else {
// Didn't find it - read a larger chunk and do everything again
continue;
}
}
$this->rootNode = $this->customRootNode;
$this->readFromChunkPos = $readFromChunkPos;
} else {
// Clear chunk to save memory, it doesn't contain the root anyway
$this->readFromChunkPos = 0;
$this->chunk = "";
continue;
}
} else {
// $$-- Valiton change: changed pattern. XML1.0 standard allows almost all
// Unicode characters even Chinese and Cyrillic.
// see:
// http://en.wikipedia.org/wiki/XML#International_use
preg_match('/<([^>\?]+)>/', $this->chunk, $matches);
// --$$
if (isset($matches[1])) {
// Found root node
$this->rootNode = $matches[1];
$this->readFromChunkPos = strpos($this->chunk, $matches[0]) + strlen($matches[0]);
} else {
// Clear chunk to save memory, it doesn't contain the root anyway
$this->readFromChunkPos = 0;
$this->chunk = "";
continue;
}
}
}
}
while (true) {
$fromChunkPos = substr($this->chunk, $this->readFromChunkPos);
preg_match('/<([^>]+)>/', $fromChunkPos, $matches);
if (isset($matches[1])) {
$element = $matches[1];
$spacePos = strpos($element, " ");
$crPos = strpos($element, "\r");
$lfPos = strpos($element, "\n");
$tabPos = strpos($element, "\t");
$aPositionsIn = array($spacePos, $crPos, $lfPos, $tabPos);
foreach ($aPositionsIn as $iPos) {
if ($iPos !== false) {
$aPositions[] = $iPos;
}
}
if(!empty($aPositions) && is_array($aPositions)) {
$minPos = min($aPositions);
}else {
$minPos = 0;
}
if ($minPos !== false && $minPos != 0) {
$sElementName = substr($element, 0, $minPos);
$endTag = "</" . $sElementName . ">";
} else {
$sElementName = $element;
$endTag = "</$sElementName>";
}
$endTagPos = false;
$lastCharPos = strlen($element) - 1;
if (substr($element, $lastCharPos) == "/") {
$endTag = "/>";
$endTagPos = $lastCharPos;
$iPos = strpos($fromChunkPos, "<");
if ($iPos !== false) {
$endTagPos += $iPos + 1;
}
}
if ($endTagPos === false) {
$endTagPos = strpos($fromChunkPos, $endTag);
}
if ($endTagPos !== false) {
$endTagEndPos = $endTagPos + strlen($endTag);
$elementWithChildren = substr($fromChunkPos, 0, $endTagEndPos);
$elementWithChildren = trim($elementWithChildren);
if($this->readFromChunkPos) {
$this->chunk = $fromChunkPos;
}
$this->chunk = substr($this->chunk, strpos($this->chunk, $endTag) + strlen($endTag));
$this->readFromChunkPos = 0;
if ($this->nodeIndex >= $this->posicionActual) {
$this->procesando = true;
break(2);
}
$this->nodeIndex++;
} else {
$this->procesando = false;
break;
}
} else {
$this->procesando = false;
break;
}
}
}
if($elementWithChildren === NULL) {
fclose($this->handle);
}
return $elementWithChildren;
}
private function readNextChunk() {
$this->chunk .= fread($this->handle, $this->chunkSize);
$this->readBytes += $this->chunkSize;
if ($this->readBytes >= $this->totalBytes) {
$this->readBytes = $this->totalBytes;
return false;
}
return true;
}
/**
* Cierra el fichero.
*/
public function cerrarArchivo() {
if($this->handle) {
fclose($this->handle);
}
}
}