This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLParser.class.php
162 lines (145 loc) · 4.04 KB
/
XMLParser.class.php
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
<?php
/*
Copyright 2019 OffTheBricks - https://github.com/mircerlancerous/XMLParser-PHP
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
class XMLParser{
private $xml;
private $pointer;
private $xmllen;
public function __construct($xml){
$this->xml = $xml;
$this->RemoveXMLHeader();
$this->xmllen = strlen($xml);
}
public function Parse(){
$this->pointer = 0;
return $this->ParseXML();
}
public function ParseObject(){
$this->pointer = 0;
$nodelist = $this->ParseXML();
$obj = new \stdClass();
if($nodelist){
foreach($nodelist as $node){
$propname = $node->tagName;
$obj->$propname = $this->NodeToObject($node);
}
}
return $obj;
}
private function RemoveXMLHeader(){
if(substr($this->xml,0,2) == "<?"){
$pos = strpos($this->xml,">");
$this->xml = substr($this->xml,$pos+1);
}
}
private function isWhiteSpace(){
if($this->pointer >= $this->xmllen){
return FALSE;
}
$char = substr($this->xml,$this->pointer,1);
return ctype_space($char);
}
private function ParseXML(){
while($this->isWhiteSpace()){
$this->pointer++;
}
$nodes = array();
//find first tag
$pos = strpos($this->xml,"<",$this->pointer);
while($pos !== FALSE){
//if the first tag is not at zero
if($pos > $this->pointer){
$node = new Node();
$node->content = trim(substr($this->xml,$this->pointer,$pos-$this->pointer));
$nodes[] = $node;
}
$this->pointer = 1 + $pos;
//find the end of the tag
$pos = strpos($this->xml,">",$this->pointer);
//check if this is the closing of a previous tag
if(substr($this->xml,$this->pointer,1) == "/"){
$this->pointer = 1 + $pos;
while($this->isWhiteSpace()){
$this->pointer++;
}
return $nodes;
}
else{
//parse tag header
$node = $this->ParseHeader(substr($this->xml,$this->pointer,$pos-$this->pointer),$selfTerminates);
$this->pointer = 1 + $pos;
if(!$selfTerminates){
$node->children = $this->ParseXML();
if(sizeof($node->children) == 1 && !$node->children[0]->tagName){
$node->content = $node->children[0]->content;
$node->children = array();
}
}
else{
while($this->isWhiteSpace()){
$this->pointer++;
}
}
$nodes[] = $node;
}
//reset and continue
$pos = strpos($this->xml,"<",$this->pointer);
}
return $nodes;
}
private function ParseHeader($head,&$selfTerminates){
$selfTerminates = FALSE;
if(substr($head,strlen($head)-1,1) == "/"){
$selfTerminates = TRUE;
$head = substr($head,0,strlen($head)-1);
}
$node = new Node();
$head = explode(" ",$head);
$node->tagName = $head[0];
$len = sizeof($head);
for($i=1; $i<$len; $i++){
if(!$head[$i]){
continue;
}
$attr = explode("=",$head[$i]);
$key = $attr[0];
$node->attributes->$key = str_replace('"',"",$attr[1]);
}
return $node;
}
private function NodeToObject($node){
if(!sizeof(get_object_vars($node->attributes)) && !sizeof($node->children)){
return $node->content;
}
$obj = $node->attributes;
$obj->content = $node->content;
foreach($node->children as $childnode){
$propname = $childnode->tagName;
$obj->$propname = $this->NodeToObject($childnode);
}
return $obj;
}
}
class Node{
public $tagName;
public $content;
public $attributes;
public $children;
public function __construct(){
$this->tagName = "";
$this->content = "";
$this->attributes = new \stdClass();
$this->children = array();
}
}
?>