-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.php
136 lines (112 loc) · 3.16 KB
/
parse.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
<?php
function log_error($a) {
;
}
$file = "haushalt2011.xml";
$info=array();
$GLOBALS['depth']=0;
function startElement($parser, $name, $attrs) {
global $info;
switch (strtolower($name)) {
case 'page':
$GLOBALS['info']['pagenumber']=$attrs['NUMBER'];
case 'text':
$GLOBALS['info']['interestingElement']['page']=$GLOBALS['info']['pagenumber'];
$GLOBALS['info']['interestingElement']['attrs']=$attrs;
$GLOBALS['info']['interestingElement']['content']="";
break;
default:
log_error("Begin unknown Element $name");
}
$GLOBALS['depth']++;
}
function endElement($parser, $name) {
switch (strtolower($name)) {
case 'page':
$GLOBALS['info']['pagenumber']=null;
break;
case 'text':
if ($GLOBALS['info']['pagenumber']=='168') { // erstmal nur eine Seite...
$GLOBALS['info']['elements'][]=$GLOBALS['info']['interestingElement'];
}
unset($GLOBALS['info']['interestingElement']);
break;
default:
log_error("Ended unknown Element $name");
}
$GLOBALS['depth']--;
}
function charData($parser,$data) {
if (isset($GLOBALS['info']['interestingElement']))
$GLOBALS['info']['interestingElement']['content'].=$data;
else
log_error("Unassociated Char Data $data");
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser,"charData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
//$tree = end(end($stack));
//pg_connect('');
echo "<div style='font-size:10px'>";
echo "\n";
$last=null;
$table=array();
$row=array();
$cell='';
foreach ($GLOBALS['info']['elements'] as $i=>$e) {
if (preg_match('/^[^a-z]*-?[0-9.,]+[^a-z]*$/',$e['content'])) {
$color='green';
} else {
$color='red';
}
extract($e['attrs']);
if ($TOP < 153 || $TOP >=400) // nur ein Test..
continue;
$RIGHT=$LEFT+$WIDTH;
$BOTTOM=$TOP+$HEIGHT;
if ($last) {
if ($WIDTH<200) {
// echo $e['content']."\n";
if ($TOP>=$last['attrs']['TOP'] && $LEFT<$last['attrs']['LEFT']-100) {
echo "down: $e[content]<br/>";
$row[]=$cell; // links darunter: naechste zeile
$table[]=$row;
$row=array();
$cell=$e['content'];
} elseif ($TOP>$last['attrs']['TOP']+3 && $LEFT<$last['attrs']['LEFT']+2) {
echo "same: $e[content]<br/>";
$cell.=$e['content']; // darunter: selbe spalte
} elseif ($TOP<=$last['attrs']['TOP']+3 && $LEFT>$last['attrs']['LEFT']+20) {
echo "right: $e[content]<br/>";
$row[]=$cell; // rechts daneben: naechste spalte
$cell=$e['content'];
}
}
} else {
echo "first: $e[content]<br/>";
$cell=$e['content']; // erste Zelle
}
$last=$e;
}
// HTML-Tabelle erzeugen: zwischenlösung, erstmal den Parser hinbekommen
echo "<table>";
foreach ($table as $row) {
echo "<tr>\n";
foreach ($row as $cell) {
echo "<td>$cell</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
echo "</div>";