fix for several problems found by Eloquence:
[lhc/web/wiklou.git] / includes / ParserXML.php
1 <?
2 /**
3 * This should one day become the XML->(X)HTML parser
4 * Based on work by Jan Hidders and Magnus Manske
5 * @package MediaWiki
6 */
7
8 /**
9 * the base class for an element
10 * @package MediaWiki
11 */
12 class element {
13 var $name = '';
14 var $attrs = array();
15 var $children = array();
16
17 function myPrint() {
18 $ret = "<ul>\n";
19 $ret .= "<li> <b> Name: </b> $this->name </li>\n";
20 // print attributes
21 $ret .= '<li> <b> Attributes: </b>';
22 foreach ($this->attrs as $name => $value) {
23 $ret .= "$name => $value; " ;
24 }
25 $ret .= " </li>\n";
26 // print children
27 foreach ($this->children as $child) {
28 if ( is_string($child) ) {
29 $ret .= "<li> $child </li>\n";
30 } else {
31 $ret .= $child->myPrint();
32 }
33 }
34 $ret .= "</ul>\n";
35 return $ret;
36 }
37 }
38
39 $ancStack = array(); // the stack with ancestral elements
40
41 // Three global functions needed for parsing, sorry guys
42 function wgXMLstartElement($parser, $name, $attrs) {
43 global $ancStack;
44
45 $newElem = new element;
46 $newElem->name = $name;
47 $newElem->attrs = $attrs;
48
49 array_push($ancStack, $newElem);
50 }
51
52 function wgXMLendElement($parser, $name) {
53 global $ancStack, $rootElem;
54 // pop element off stack
55 $elem = array_pop ($ancStack);
56 if (count ($ancStack) == 0)
57 $rootElem = $elem;
58 else
59 // add it to its parent
60 array_push ($ancStack[count($ancStack)-1]->children, $elem);
61 }
62
63 function wgXMLcharacterData($parser, $data) {
64 global $ancStack;
65 $data = trim ($data); // Don't add blank lines, they're no use...
66 // add to parent if parent exists
67 if ( $ancStack && $data != "" ) {
68 array_push ($ancStack[count($ancStack)-1]->children, $data);
69 }
70 }
71
72
73 /**
74 * Here's the class that generates a nice tree
75 * package parserxml
76 * @package MediaWiki
77 */
78 class xml2php {
79
80 function &scanFile( $filename ) {
81 global $ancStack, $rootElem;
82 $ancStack = array();
83
84 $xml_parser = xml_parser_create();
85 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
86 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
87 if (!($fp = fopen($filename, 'r'))) {
88 die('could not open XML input');
89 }
90 while ($data = fread($fp, 4096)) {
91 if (!xml_parse($xml_parser, $data, feof($fp))) {
92 die(sprintf("XML error: %s at line %d",
93 xml_error_string(xml_get_error_code($xml_parser)),
94 xml_get_current_line_number($xml_parser)));
95 }
96 }
97 xml_parser_free($xml_parser);
98
99 // return the remaining root element we copied in the beginning
100 return $rootElem;
101 }
102
103 function scanString ( $input ) {
104 global $ancStack, $rootElem;
105 $ancStack = array();
106
107 $xml_parser = xml_parser_create();
108 xml_set_element_handler ($xml_parser, 'wgXMLstartElement', 'wgXMLendElement');
109 xml_set_character_data_handler ($xml_parser, 'wgXMLcharacterData');
110
111 if (!xml_parse ($xml_parser, $input, true)) {
112 die (sprintf ("XML error: %s at line %d",
113 xml_error_string(xml_get_error_code($xml_parser)),
114 xml_get_current_line_number($xml_parser)));
115 }
116 xml_parser_free ($xml_parser);
117
118 // return the remaining root element we copied in the beginning
119 return $rootElem;
120 }
121
122 }
123
124 /* Example code:
125
126 $w = new xml2php;
127 $filename = 'sample.xml';
128 $result = $w->scanFile( $filename );
129 print $result->myPrint();
130 */
131
132 ?>