|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleXmlReader; |
|
4
|
|
|
|
|
5
|
|
|
class SimpleXmlReader |
|
6
|
|
|
{ |
|
7
|
|
|
const RETURN_DOM = 'RETURN_DOM'; |
|
8
|
|
|
const RETURN_SIMPLE_XML = 'RETURN_SIMPLE_XML'; |
|
9
|
|
|
const RETURN_INNER_XML_STRING = 'RETURN_INNER_XML_STRING'; |
|
10
|
|
|
const RETURN_OUTER_XML_STRING = 'RETURN_OUTER_XML_STRING'; |
|
11
|
|
|
|
|
12
|
|
|
protected $xmlReader; |
|
13
|
|
|
|
|
14
|
|
|
protected function __construct() |
|
15
|
|
|
{ |
|
16
|
|
|
$this->xmlReader = new ExceptionThrowingXMLReader(); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public static function autoOpenXML($path, $encoding = 'UTF-8', $options = 0) |
|
20
|
|
|
{ |
|
21
|
|
|
if (strtolower(substr($path, -3)) == '.gz') { |
|
22
|
|
|
return self::openGzippedXML($path, $encoding, $options); |
|
23
|
|
|
} else { |
|
24
|
|
|
return self::openXML($path, $encoding, $options); |
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public static function openXML($path, $encoding = 'UTF-8', $options = 0) |
|
29
|
|
|
{ |
|
30
|
|
|
$simpleXmlReader = new self(); |
|
31
|
|
|
$simpleXmlReader->xmlReader->open($path, $encoding, $options); |
|
32
|
|
|
return $simpleXmlReader; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public static function openGzippedXML($path, $encoding = 'UTF-8', $options = 0) |
|
36
|
|
|
{ |
|
37
|
|
|
return self::openXML("compress.zlib://$path", $encoding, $options); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function openFromString($source, $encoding = 'UTF-8', $options = 0) |
|
41
|
|
|
{ |
|
42
|
|
|
$simpleXmlReader = new self(); |
|
43
|
|
|
$simpleXmlReader->xmlReader->XML($source, $encoding, $options); |
|
44
|
|
|
return $simpleXmlReader; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function path($path, $returnType = self::RETURN_SIMPLE_XML) |
|
48
|
|
|
{ |
|
49
|
|
|
return new PathIterator($this->xmlReader, $path, $returnType); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|