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