-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlparse.php
218 lines (183 loc) · 5.54 KB
/
xmlparse.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
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
<?
class XMLToArray
{
# en klass jag hittade på
# http://www.devarticles.com/c/a/PHP/Converting-XML-Into-a-PHP-Data-Structure/
//-----------------------------------------
var $parser;
var $node_stack = array();
var $errormsg = "";
var $curpos = array();
//-----------------------------------------
/** PUBLIC
* Parse a text string containing valid XML into a multidimensional array
* located at rootnode.
*/
function parse($xmlstring="")
{
// set up a new XML parser to do all the work for us
$this->parser = xml_parser_create("UTF-8");
xml_set_object($this->parser, $this);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($this->parser, "startElement", "endElement");
xml_set_character_data_handler($this->parser, "characterData");
// Build a Root node and initialize the node_stack...
$this->node_stack = array();
$this->startElement(null, "root", array());
// parse the data and free the parser...
xml_parse($this->parser, preg_replace("/&(?!amp;)/", "&", $xmlstring));
$this->errormsg = xml_error_string(xml_get_error_code($this->parser));
$this->curpos["line"] = xml_get_current_line_number($this->parser);
$this->curpos["column"] = xml_get_current_column_number($this->parser);
xml_parser_free($this->parser);
// recover the root node from the node stack
$rnode = array_pop($this->node_stack);
$rnode = array_shift($rnode["_ELEMENTS"]);
// return the root node...
return($rnode);
}
//-----------------------------------------
/** PROTECTED
* Start a new Element. This means we push the new element onto the stack
* and reset it's properties.
*/
function startElement($parser, $name, $attrs)
{
// create a new node...
$node = array();
$node["_NAME"] = $name;
foreach ($attrs as $key => $value) {
$node[$key] = $value;
}
$node["_DATA"] = "";
$node["_ELEMENTS"] = array();
// add the new node to the end of the node stack
array_push($this->node_stack, $node);
}
//-----------------------------------------
/** PROTECTED
* End an element. This is done by popping the last element from the
* stack and adding it to the previous element on the stack.
*/
function endElement($parser, $name)
{
// pop this element off the node stack
$node = array_pop($this->node_stack);
$node["_DATA"] = trim($node["_DATA"]);
// and add it an an element of the last node in the stack...
$lastnode = count($this->node_stack);
array_push($this->node_stack[$lastnode-1]["_ELEMENTS"], $node);
}
//-----------------------------------------
/** PROTECTED
* Collect the data onto the end of the current chars.
*/
function characterData($parser, $data)
{
// add this data to the last node in the stack...
$lastnode = count($this->node_stack);
$this->node_stack[$lastnode-1]["_DATA"] .= $data;
}
}
//-----------------------------------------
function xml_get_all_elements($root, $element)
{
$elements = array();
foreach($root["_ELEMENTS"] as $folder)
if($folder["_NAME"] == $element)
array_push($elements, $folder);
return $elements;
}
function xml_get_element_data($root, $element)
{
$e = xml_get_element($root, $element);
return decode_html_entities($e["_DATA"]);
}
function xml_get_element($root, $element)
{
if(count($root["_ELEMENTS"]) == 0)
die("no element '$element' found! \n");
foreach($root["_ELEMENTS"] as $folder)
if($folder["_NAME"] == $element)
return $folder;
return $root;
}
# parsar Atom-formatet till en array (endast titel och url)
function parse_atom($xmldata)
{
$xmltoarray = new XMLToArray();
$root_node = $xmltoarray->parse($xmldata);
if(!isset($root_node["_ELEMENTS"]))
return false;
$atomdata = array();
foreach($root_node["_ELEMENTS"] as $folder)
if($folder["_NAME"] == "entry")
foreach($folder["_ELEMENTS"] as $item)
if($item["_NAME"] == "link" and $item["type"] == "text/html")
{
$data = array();
$data["url"] = $item["href"];
$data["title"] = $item["title"];
array_push($atomdata, $data);
}
return $atomdata;
}
# parsar RDF-formatet till en array (gör ingen versionskontroll)
function parse_rdf($xmldata)
{
$xmltoarray = new XMLToArray();
$root_node = $xmltoarray->parse($xmldata);
$rssdata = array();
foreach($root_node["_ELEMENTS"] as $folder)
{
if($folder["_NAME"] == "item")
{
$item = array();
foreach($folder["_ELEMENTS"] as $file)
{
if($file["_NAME"] == "title")
$item["title"] = $file["_DATA"];
elseif($file["_NAME"] == "description")
$item["description"] = $file["_DATA"];
elseif($file["_NAME"] == "link")
$item["link"] = $file["_DATA"];
elseif($file["_NAME"] == "dc:date")
{
$date = str_replace(array("T", "Z"), " ", $file["_DATA"]);
$item["date"] = strtotime($date);
}
}
array_push($rssdata, $item);
}
}
return $rssdata;
}
# parsar RSS 2.0-formatet till en array (gör ingen versionskontroll)
function parse_rss($xmldata)
{
$xmltoarray = new XMLToArray();
$root_node = $xmltoarray->parse($xmldata);
$channel = xml_get_element($root_node, "channel");
$rssdata = array();
foreach($channel["_ELEMENTS"] as $folder)
{
if($folder["_NAME"] == "item")
{
$item = array();
foreach($folder["_ELEMENTS"] as $file)
{
if($file["_NAME"] == "title")
$item["title"] = $file["_DATA"];
elseif($file["_NAME"] == "description")
$item["description"] = $file["_DATA"];
elseif($file["_NAME"] == "link")
$item["link"] = $file["_DATA"];
elseif($file["_NAME"] == "pubDate")
$item["date"] = strtotime($file["_DATA"]);
}
array_push($rssdata, $item);
}
}
return $rssdata;
}
?>